2010-12-13 22 views
20

Tôi có bố cục sử dụng EditText để cho phép người dùng tìm kiếm cơ sở dữ liệu và điền một ListView. EditText là khoảng 2/3 đường từ trên cùng của màn hình (được đặt trên một ImageView, và theo sau là một số văn bản giới thiệu.)SoftKeyboard ẩn EditText

Vấn đề là bàn phím mềm ẩn EditText, vì vậy người dùng có thể ' t xem những gì anh ấy đang gõ. (Tôi đã tắt đề xuất tự động.)

Tôi đã thử LinearLayout, RelativeLayout, paddings và các căn chỉnh/trung tâm khác nhau, nhưng tôi vẫn không thể làm cho nó hoạt động đúng. EditText bị ẩn, bị đẩy ra khỏi đầu màn hình, hoặc bị "đánh bóng" và bị méo.

Đề xuất ???

Một cách giải quyết khác là di chuyển EditText lên đầu màn hình. Tuy nhiên, điều này lệch khỏi thiết kế đồ họa mà tôi đã được đưa ra.

Một cách giải quyết khác có thể là để tôi làm cho bàn phím mềm mở ở chế độ toàn màn hình (không chắc chắn như thế nào, mặc dù). Điều này sẽ vẫn ẩn EditText, nhưng sau đó tôi có thể bật lại đề xuất tự động để người dùng có thể xem những gì anh ấy đang nhập ... sắp xếp ... vì anh ấy chỉ có thể xem các đề xuất cho nội dung anh ấy đang nhập.

Đây là nỗ lực mới nhất của tôi. Xem "introFrame".

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="fill_parent" 
android:orientation="vertical"> 

<LinearLayout android:id="@+id/titleContainer" 
    android:orientation="horizontal" android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView android:text="@string/title_string" 
    android:textSize="15sp" android:textColor="#FFFFFF" 
    android:textStyle="bold" android:paddingLeft="5dp" 
    android:layout_height="fill_parent" android:layout_width="wrap_content" /> 
</LinearLayout> 

<FrameLayout 
android:id="@+id/introFrame" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:gravity="center_horizontal" > 
<ImageView 
    android:src="@drawable/main_search_image" 
    android:scaleType="center" 
    android:layout_gravity="center" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"/> 
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:paddingTop="140dp" > 
    <LinearLayout android:id="@+id/introSearchContainer" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" > 
    <LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" > 
    <EditText android:id="@+id/intro_search_box" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:hint="  Enter keyword  " 
    android:imeOptions="actionGo" 
    android:inputType="textFilter" 
    android:maxLines="1" /> 
    </LinearLayout> 
    <LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 
    <Button android:id="@+id/intro_search_button" 
    android:background="@drawable/custom_button_go" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
    </LinearLayout> 
    </LinearLayout> 
    <TextView 
    android:text="@string/search_intro" 
    android:textSize="15sp" 
    android:textColor="#FFFFFF" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" /> 
</LinearLayout> 
</FrameLayout> 

<LinearLayout android:id="@+id/listContainer" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ListView android:id="@+id/itemlist" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:cacheColorHint="#00000000" /> 
    <TextView android:text="No data found" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:gravity="center" 
    android:textSize="16sp" android:textColor="#FFFFFF" android:id="@+id/android:empty" /> 
</LinearLayout> 

</LinearLayout> 
+0

Đây là cách giải quyết sự cố của tôi http://stackoverflow.com/a/29408773/3595514 – Rajendra

Trả lời

56

Điều bạn đang tìm kiếm là thuộc tính của windowSoftInputMode của Hoạt động. Bạn đặt này trong tập tin AndroidManifest.xml của bạn, và cung cấp cho nó một giá trị như:

  • adjustResize: "cửa sổ chính của hoạt động luôn thay đổi kích cỡ để nhường chỗ cho bàn phím mềm trên màn hình."

  • adjustPan: "Cửa sổ chính của hoạt động không được thay đổi kích thước để nhường chỗ cho bàn phím mềm. Thay vào đó, nội dung của cửa sổ được tự động quét sao cho tiêu điểm hiện tại không bao giờ bị che khuất bởi bàn phím và người dùng luôn có thể thấy Điều này thường ít được mong muốn hơn là thay đổi kích thước, bởi vì người dùng có thể cần phải đóng bàn phím mềm để vào và tương tác với các phần bị che khuất của cửa sổ. "

điều chỉnhResize có thể sẽ hoạt động cho bạn, miễn là bạn bao bọc bố cục trong ScrollView. Nó có thể có tác động tiêu cực nếu bạn có một hình ảnh bitmap trong nền, vì nó sẽ được thay đổi kích cỡ là tốt, trong trường hợp đó bạn có thể muốn sử dụng adjustPan thay thế.

<activity android:windowSoftInputMode="adjustResize" /> 

hoặc

<activity android:windowSoftInputMode="adjustPan" /> 

biết thêm thông tin có sẵn tại liên kết ở trên.

+5

không làm việc cho tôi trong một Fragment như một đứa trẻ của một FragmentActivity. – Bobbelinio

+0

tính năng này không hoạt động khi ứng dụng đang chạy ở chế độ toàn màn hình –

1

thử đưa ra các chỉnh sửa văn bản một layout_weight (tức layout_weight = 1), điều này có thể có một số hiệu ứng khác vào các mặt hàng bố trí khác mà bạn có thể phải làm việc thông qua, nhưng điều này có thể giúp nó ở lại có thể nhìn thấy khi mềm bàn phím bật lên

13

Điều gì đã làm việc cho tôi là giảm LinearLayout cấp cao nhất của tôi trong một scrollView (ScrollView chỉ có thể có một con). Điều này cho phép toàn bộ hoạt động/biểu mẫu cuộn lên và không làm lộn xộn EditText trong tiêu điểm.

Trước tiên, tôi thiết lập trong hoạt động của tôi:

<activity android:windowSoftInputMode="adjustPan" /> 

Sau đó, tôi đã làm điều scrollview Tôi đang nói về:

<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- STUFF --> 

    </LinearLayout> 
</ScrollView> 
+0

Đã không hoạt động đối với tôi trên ICS. Bất kỳ ý tưởng nếu thêm stateHidden để windowSoftInputMode có thể gây ra vấn đề này? –

+0

+1 Cuối cùng! 'Scrollview' là chìa khóa cho tôi. Cảm ơn! – rendon

+0

không làm việc cho tôi trong một mảnh vỡ như một đứa trẻ của một FragmentActivity. – Bobbelinio

2

chỉ cần sử dụng sau đây trong manifest ...

<activity 
     android:windowSoftInputMode="adjustPan">