简体   繁体   English

Android:键盘与 EditText 重叠(带有打印屏幕)

[英]Android: Keyboard overlaps with the EditText (with printscreens)

I have an EditText (that the user can type numbers in), so when the user clicks on the EditText text box a keyboard with numbers is opened.我有一个 EditText(用户可以输入数字),所以当用户点击 EditText 文本框时,会打开一个带有数字的键盘。

这是单击文本框时的外观

as you can see the keyboard hides a small part of the text box.如您所见,键盘隐藏了文本框的一小部分。

But when I press a key, for example, 0, it looks ok.但是当我按下一个键时,例如 0,它看起来没问题。这是单击 0 后的样子

Is there anything I can do (besides putting the EditText higher) so it will looks like it does in the second picture?有什么我可以做的(除了将 EditText 放在更高的位置)使它看起来像第二张图片中的那样?

Edit: the .xml code:编辑: .xml 代码:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:weightSum="1">
    <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content">
    <LinearLayout android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_alignParentRight="true">
        <android.widget.CheckedTextView android:id="@+id/checkedTextView1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:textSize="17sp" android:text="@string/toString"></android.widget.CheckedTextView>
        <AutoCompleteTextView android:layout_height="wrap_content" android:id="@+id/autoCompleteTextView1" android:layout_width="fill_parent" android:text="@string/emptyString" android:textSize="17sp" android:gravity="top|left" android:minHeight="62dp">
            <requestFocus></requestFocus>
        </AutoCompleteTextView>
        <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout2">
            <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="0.33333333333" android:text="@string/contactsString" android:textSize="17sp" android:id="@+id/contactsButton"></Button>
            <Button android:layout_weight="0.33333333333" android:layout_height="wrap_content" android:text="@string/groupsString" android:layout_width="fill_parent" android:id="@+id/groupsButton" android:textSize="17sp"></Button>
            <Button android:layout_weight="0.33333333333" android:layout_height="wrap_content" android:text="@string/favouritesString" android:layout_width="fill_parent" android:id="@+id/button3" android:textSize="17sp"></Button>
        </LinearLayout>
        <TextView android:id="@+id/textView1" android:text="@string/messageString" android:layout_height="wrap_content" android:textSize="17sp" android:layout_width="fill_parent"></TextView>
        <EditText android:layout_height="wrap_content" android:id="@+id/editText1" android:layout_width="fill_parent" android:gravity="top|left" android:minHeight="105dp"></EditText>
        <TextView android:id="@+id/textView2" android:text="@string/repetition" android:layout_height="wrap_content" android:layout_width="fill_parent" android:textSize="17sp"></TextView>
        <Spinner android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/spinner"></Spinner>
        <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout3" android:layout_width="fill_parent">
            <ImageView android:src="@drawable/button_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView1" android:layout_weight="0.1"></ImageView>
            <EditText android:layout_height="wrap_content" android:id="@+id/timeET" android:inputType="number" android:layout_width="wrap_content" android:layout_weight="0.4"></EditText>
            <ImageView android:src="@drawable/button_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView2" android:layout_weight="0.1"></ImageView>
            <EditText android:layout_height="wrap_content" android:id="@+id/dateET" android:inputType="number" android:layout_width="wrap_content" android:layout_weight="0.4" android:layout_marginRight="3dp"></EditText>
        </LinearLayout>
        <RelativeLayout android:id="@+id/relativeLayout2" android:layout_width="fill_parent" android:layout_height="fill_parent">
            <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/linearLayout4" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentLeft="true">
                <Button android:layout_weight="0.5" android:layout_height="wrap_content" android:text="@string/button_ok" android:layout_width="fill_parent" android:id="@+id/button4" android:textSize="17sp"></Button>
                <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/button5" android:layout_weight="0.5" android:text="@string/button_cancel" android:textSize="17sp"></Button>
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

</LinearLayout>

I have tried your XML and yes you are right the problem occur.我已经尝试过您的 XML,是的,您是对的,问题发生了。

To solve the problem I have written this line in my MainActivity.java hope this help to you,And put the layout XML in ScrollView.为了解决这个问题,我在我的 MainActivity.java 中写了这一行希望这对你有帮助,并将布局 XML 放在 ScrollView 中。

Activity活动

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.temp);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        final EditText time = (EditText)findViewById(R.id.timeET);
        time.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                time.requestLayout();
                MyActivity.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);

                return false;
            }
        });
        final EditText date = (EditText)findViewById(R.id.dateET);
        date.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                time.requestLayout();
                MyActivity.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);

                return false;
            }
        });
         }

And The XML is Like,XML就像,

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

        <ScrollView android:id="@+id/scrollView1"
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent" 
        android:weightSum="1">
---
---
---
        </ScrollView> 
</LinearLayout> 

Change to ScrollView in this way:以这种方式更改为 ScrollView:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:weightSum="1" >
        <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <LinearLayout
                android:layout_width="wrap_content"
                android:orientation="vertical"
                android:layout_height="wrap_content"
                android:id="@+id/linearLayout1"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true" >
                <android.widget.CheckedTextView
                    android:id="@+id/checkedTextView1"
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"
                    android:textSize="17sp"
                    android:text="@string/toString" />
                <AutoCompleteTextView
                    android:layout_height="wrap_content"
                    android:id="@+id/autoCompleteTextView1"
                    android:layout_width="fill_parent"
                    android:text="@string/emptyString"
                    android:textSize="17sp"
                    android:gravity="top|left"
                    android:minHeight="62dp" >
                    <requestFocus></requestFocus>
                </AutoCompleteTextView>
                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/linearLayout2" >
                    <Button
                        android:layout_height="wrap_content"
                        android:layout_width="fill_parent"
                        android:layout_weight="0.33333333333"
                        android:text="@string/contactsString"
                        android:textSize="17sp"
                        android:id="@+id/contactsButton" />
                    <Button
                        android:layout_weight="0.33333333333"
                        android:layout_height="wrap_content"
                        android:text="@string/groupsString"
                        android:layout_width="fill_parent"
                        android:id="@+id/groupsButton"
                        android:textSize="17sp" />
                    <Button
                        android:layout_weight="0.33333333333"
                        android:layout_height="wrap_content"
                        android:text="@string/favouritesString"
                        android:layout_width="fill_parent"
                        android:id="@+id/button3"
                        android:textSize="17sp" />
                </LinearLayout>
                <TextView
                    android:id="@+id/textView1"
                    android:text="@string/messageString"
                    android:layout_height="wrap_content"
                    android:textSize="17sp"
                    android:layout_width="fill_parent" />
                <EditText
                    android:layout_height="wrap_content"
                    android:id="@+id/editText1"
                    android:layout_width="fill_parent"
                    android:gravity="top|left"
                    android:minHeight="105dp" />
                <TextView
                    android:id="@+id/textView2"
                    android:text="@string/repetition"
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"
                    android:textSize="17sp" />
                <Spinner
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/spinner" />
                <LinearLayout
                    android:layout_height="wrap_content"
                    android:id="@+id/linearLayout3"
                    android:layout_width="fill_parent" >
                    <ImageView
                        android:src="@drawable/button_time"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/imageView1"
                        android:layout_weight="0.1" />
                    <EditText
                        android:layout_height="wrap_content"
                        android:id="@+id/timeET"
                        android:inputType="number"
                        android:layout_width="wrap_content"
                        android:layout_weight="0.4" />
                    <ImageView
                        android:src="@drawable/button_date"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/imageView2"
                        android:layout_weight="0.1" />
                    <EditText
                        android:layout_height="wrap_content"
                        android:id="@+id/dateET"
                        android:inputType="number"
                        android:layout_width="wrap_content"
                        android:layout_weight="0.4"
                        android:layout_marginRight="3dp" />
                </LinearLayout>
                <RelativeLayout
                    android:id="@+id/relativeLayout2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/linearLayout4"
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentLeft="true" >
                        <Button
                            android:layout_weight="0.5"
                            android:layout_height="wrap_content"
                            android:text="@string/button_ok"
                            android:layout_width="fill_parent"
                            android:id="@+id/button4"
                            android:textSize="17sp" />
                        <Button
                            android:layout_height="wrap_content"
                            android:layout_width="fill_parent"
                            android:id="@+id/button5"
                            android:layout_weight="0.5"
                            android:text="@string/button_cancel"
                            android:textSize="17sp" />
                    </LinearLayout>
                </RelativeLayout>
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</ScrollView>

put the entire view inside a ScrollView and set the android:windowSoftInputMode = adjustPan it will do the trick.将整个视图放在 ScrollView 中并设置android:windowSoftInputMode = adjustPan就可以了。

you just need to add this piece of code,你只需要添加这段代码,

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

Your linear layout here....

</ScrollView>

I have tested it on my HTC Desire and its working fine for me hope it will work for you too.我已经在我的 HTC Desire 上测试过它,它对我来说工作正常,希望它也对你有用。

Set android:windowSoftInputMode on the Activity to "adjustPan" :将 Activity 上的android:windowSoftInputMode设置为"adjustPan"

The activity's main window is not resized to make room for the soft keyboard.活动的主窗口没有调整大小来为软键盘腾出空间。 Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing.相反,窗口的内容会自动平移,因此当前焦点永远不会被键盘遮挡,用户始终可以看到他们正在键入的内容。

Beware of one potential bug when using this technique with a fullscreen activity.在全屏活动中使用此技术时,请注意一个潜在的错误

This is a much simpler fix than the accepted answer.这比接受的答案要简单得多。 The key is the <item name="android:windowSoftInputMode">adjustUnspecified</item> line.关键是<item name="android:windowSoftInputMode">adjustUnspecified</item>行。 Add it to your styles.xml:将它添加到你的styles.xml:

<style name="AppTheme" parent="@android:Theme.Holo.Light.DarkActionBar">
    <item name="android:alertDialogTheme">@style/iconPopUpDialogTheme</item>
</style>

<style name="DialogAppTheme" parent="AppTheme">
    <item name="android:dialogTheme">@style/iconPopUpDialogTheme</item>
</style>

<style name="PopUpDialogTheme">
    <item name="android:windowSoftInputMode">adjustUnspecified</item>
</style>

You can give some hints to the system on how to handle this via android:windowSoftInputMode element on the declared activity in the AndroidManifest.您可以通过 AndroidManifest 中声明的活动上的 android:windowSoftInputMode 元素向系统提供有关如何处理此问题的提示。 Try the "adjustResize" value.试试“adjustResize”值。

android:windowSoftInputMode android:windowSoftInputMode

try changing linear layout to scroll view...so that if keyboard comes above editt text user can scroll and type...尝试将线性布局更改为滚动视图...这样,如果键盘位于编辑文本上方,则用户可以滚动并键入...

 <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1">
<RelativeLayout android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent" android:layout_height="wrap_content">
    <LinearLayout android:layout_width="wrap_content"
        android:orientation="vertical" android:layout_height="wrap_content"
        android:id="@+id/linearLayout1" android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" android:layout_alignParentTop="true"
        android:layout_alignParentRight="true">
        <android.widget.CheckedTextView
            android:id="@+id/checkedTextView1" android:layout_height="wrap_content"
            android:layout_width="fill_parent" android:textSize="17sp"></android.widget.CheckedTextView>
        <AutoCompleteTextView android:layout_height="wrap_content"
            android:id="@+id/autoCompleteTextView1" android:layout_width="fill_parent"
            android:textSize="17sp" android:gravity="top|left"
            android:minHeight="62dp">
            <requestFocus></requestFocus>
        </AutoCompleteTextView>
        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:id="@+id/linearLayout2">
            <Button android:layout_height="wrap_content"
                android:layout_width="fill_parent" android:layout_weight="0.33333333333"
                android:textSize="17sp" android:id="@+id/contactsButton"></Button>
            <Button android:layout_weight="0.33333333333"
                android:layout_height="wrap_content" android:layout_width="fill_parent"
                android:id="@+id/groupsButton" android:textSize="17sp"></Button>
            <Button android:layout_weight="0.33333333333"
                android:layout_height="wrap_content" android:layout_width="fill_parent"
                android:id="@+id/button3" android:textSize="17sp"></Button>
        </LinearLayout>
        <TextView android:id="@+id/textView1" android:layout_height="wrap_content"
            android:textSize="17sp" android:layout_width="fill_parent"></TextView>
        <EditText android:layout_height="wrap_content" android:id="@+id/editText1"
            android:layout_width="fill_parent" android:gravity="top|left"
            android:minHeight="105dp"></EditText>
        <TextView android:id="@+id/textView2" android:layout_height="wrap_content"
            android:layout_width="fill_parent" android:textSize="17sp"></TextView>
        <Spinner android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:id="@+id/spinner"></Spinner>
        <LinearLayout android:layout_height="wrap_content"
            android:id="@+id/linearLayout3" android:layout_width="fill_parent">
            <ImageView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:id="@+id/imageView1"
                android:layout_weight="0.1"></ImageView>
            <EditText android:layout_height="wrap_content" android:id="@+id/timeET"
                android:inputType="number" android:layout_width="wrap_content"
                android:layout_weight="0.4"></EditText>
            <ImageView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:id="@+id/imageView2"
                android:layout_weight="0.1"></ImageView>
            <EditText android:layout_height="wrap_content" android:id="@+id/dateET"
                android:inputType="number" android:layout_width="wrap_content"
                android:layout_weight="0.4" android:layout_marginRight="3dp"></EditText>
        </LinearLayout>
        <RelativeLayout android:id="@+id/relativeLayout2"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
            <LinearLayout android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:id="@+id/linearLayout4"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentLeft="true">
                <Button android:layout_weight="0.5" android:layout_height="wrap_content"
                    android:layout_width="fill_parent" android:id="@+id/button4"
                    android:textSize="17sp"></Button>
                <Button android:layout_height="wrap_content"
                    android:layout_width="fill_parent" android:id="@+id/button5"
                    android:layout_weight="0.5" android:textSize="17sp"></Button>
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

please make necesscery changes...i have removed strings and drawable src for my convience..u need to change first linear layout to scrollview..try without this android:windowSoftInputMode = adjustPan请进行必要的更改...为了方便起见,我删除了字符串和可绘制的 src ..你需要将第一个线性布局更改为滚动视图..尝试没有这个 android:windowSoftInputMode = adjustPan

在您的Manifest.xml文件中添加这个简单的行:

android:windowSoftInputMode="adjustResize|stateHidden"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM