【问题标题】:Android: Keyboard overlaps with the EditText (with printscreens)Android:键盘与 EditText 重叠(带有打印屏幕)
【发布时间】:2011-11-03 19:34:11
【问题描述】:

我有一个 EditText(用户可以在其中输入 numbers), 所以当用户点击 EditText 文本框时,会打开一个带有数字的键盘。

如您所见,键盘隐藏了一小部分文本框。

但是当我按下一个键时,例如,0,它看起来没问题。

我能做些什么(除了将 EditText 放在更高的位置)让它看起来像在第二张图片中那样吗?

编辑: .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>

【问题讨论】:

    标签: android


    【解决方案1】:

    我已经尝试了你的 XML,是的,你是对的,问题发生了。

    为了解决这个问题我在我的MainActivity.java中写了这行希望对你有帮助,并将布局XML放在ScrollView中。

    活动

    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;
                }
            });
             }
    

    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> 
    

    【讨论】:

    • 我将在今天晚些时候或明天早上尝试这个,我会告诉你它是如何工作的。谢谢!
    • p.s : 你的意思是写 date.requestLayout();在 date.setOnTouchListener ?
    • @Frankenstein 呵呵,非常感谢您的评论。我现在想删除我的答案,但你的评论阻止了我!无论如何,我粗略地回答了我认为根 ScrollView 可以解决问题。看来我错了。为什么我们在您的答案中需要根 LinearLayout?我真的不太了解这个问题的细节,但我认为 OnFocus 听众是正确的选择。无论如何,我不能评论我周末要享受的细节。再次感谢你 !! +1
    • 我尝试使用代码(用于 .xml 和 java),不幸的是,另一个答案中出现的问题在这里是正确的:时间和日期 EditText 之间的空间现在消失了,所以按钮更高。此外,我在编辑文本中的文本也不见了。 ://
    • @MKJParekh 我的问题通过 Padding 解决了 :)。我给edittext一个padding = 10dp,它对我来说很好。顺便谢谢你。
    【解决方案2】:

    这样改成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>
    

    【讨论】:

    • button_ok 和 button4(即取消按钮)不再位于屏幕底部,键盘无缘无故打开...
    • 您在autoCompleteTextView1 上有&lt;requestFocus&gt;&lt;/requestFocus&gt;。所以键盘必须打开! button_okbutton4 是布局中的最后一件事。所以它们必须位于屏幕底部。
    • 发布的代码没有打开键盘。确实,这些按钮比屏幕上的所有其他东西都低,但 EditText 的时间和日期之间的空间现在消失了,所以按钮更高了。
    • @Sherif 你真棒!我很佩服你,并希望您对我上面的回答发表评论..请..还想提及您的博客以进行链接..帮了我很多..谢谢:)
    【解决方案3】:

    将整个视图放入 ScrollView 并设置 android:windowSoftInputMode = adjustPan 即可。

    你只需要添加这段代码,

    <?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>
    

    我已经在我的 HTC Desire 上对其进行了测试,它对我来说运行良好,希望它也对你有用。

    【讨论】:

    • 你能帮助我如何使布局成为 ScrollView 吗?我将在几秒钟内添加 .xml 代码
    【解决方案4】:

    将Activity上的android:windowSoftInputMode设置为"adjustPan"

    Activity 的主窗口没有调整大小来为软键盘腾出空间。相反,窗口的内容会自动平移,因此当前焦点永远不会被键盘遮挡,用户始终可以看到他们正在输入的内容。

    在全屏活动中使用此技术时请注意one potential bug

    【讨论】:

    • 可以在here 找到对不同输入模式(包括上面建议的模式)的更直观解释。
    • 看起来和第一张照片一模一样:/
    【解决方案5】:

    您可以通过 AndroidManifest 中声明的活动上的 android:windowSoftInputMode 元素向系统提供一些提示,说明如何处理此问题。试试“adjustResize”值。

    android:windowSoftInputMode

    【讨论】:

    • 这完全隐藏了 EditText 文本框
    【解决方案6】:

    这比接受的答案要简单得多。关键是&lt;item name="android:windowSoftInputMode"&gt;adjustUnspecified&lt;/item&gt; 行。将其添加到您的 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>
    

    【讨论】:

      【解决方案7】:

      尝试将线性布局更改为滚动视图...这样如果键盘位于编辑文本上方,用户可以滚动和输入...

       <?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>
      

      请进行必要的更改...为了方便起见,我删除了字符串和可绘制的 src..你需要先更改 滚动视图的线性布局..尝试没有这个 android:windowSoftInputMode = 调整盘

      【讨论】:

        【解决方案8】:

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

        android:windowSoftInputMode="adjustResize|stateHidden"
        

        【讨论】:

        • 虽然此代码可能会为问题提供解决方案,但最好添加有关其工作原理/方式的上下文。这可以帮助未来的用户学习并将这些知识应用到他们自己的代码中。解释代码时,您也可能会以赞成票的形式从用户那里获得积极的反馈。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多