【问题标题】:EditText without keyboard in landscape mode横向模式下没有键盘的EditText
【发布时间】:2012-08-24 18:53:38
【问题描述】:

我打算显示一个带有 EditText 的弹出窗口,一旦显示键盘就会自动激活:

public void showPopup(View view) // a button invoke
{
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View content = inflater.inflate(R.layout.popup, null);
    PopupWindow pw = new PopupWindow(content, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true);
    pw.setBackgroundDrawable(new BitmapDrawable());
    pw.showAtLocation(view, Gravity.TOP, 0, 0);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    (new Handler()).post(new Runnable() {
        public void run() {
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
        }
    });
}

popup.xml 是:

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

>
<EditText android:id="@+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Hello Android!!"
android:hint="Filling in it!!"
android:layout_weight="1"
android:selectAllOnFocus="true"
android:focusable="true"
android:focusableInTouchMode="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go!" 

/>
</LinearLayout>

在纵向模式下效果很好,但在添加以下代码后在横向模式下效果不佳:

protected void onResume() {
    if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
}

谁能给出一些解决方案??

【问题讨论】:

    标签: android keyboard landscape


    【解决方案1】:

    编辑你的 popup.xml 文件

     <LinearLayout............
      .....
      android:focusable="true"
     android:focusableInTouchMode="true" ...
    

     <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="horizontal"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:focusable="true"
     android:focusableInTouchMode="true"
     >
     <EditText android:id="@+id/edit_message"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:text="Hello Android!!"
     android:hint="Filling in it!!"
     android:layout_weight="1"
     android:selectAllOnFocus="true"
     android:focusable="true"
     android:focusableInTouchMode="true"
     />
     <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Go!" 
     />
     </LinearLayout>
    

    【讨论】:

    • 与您的 popup.xml 文件相同,但仅在 LinearLayout 中添加 (android:focusable="true" 和 android:focusableInTouchMode="true")...
    【解决方案2】:

    在 EditText requestFocus 处添加您的 xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    
    >
    <EditText android:id="@+id/edit_message"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Hello Android!!"
    android:hint="Filling in it!!"
    android:layout_weight="1"
    android:selectAllOnFocus="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    ><requestFocus/></EditText>
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Go!" 
    
    />
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      • 2018-02-01
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多