首页 文章

以编程方式隐藏/显示Android软键盘[复制]

提问于
浏览
219

可能重复:关闭/隐藏Android软键盘

首先,我已经看到了this线程 . 我试过那里接受的方法..但没有什么对我有用..

我的应用程序中有两个屏幕 .

  • 第一个有2个EditText - 一个用于用户名,一个用于密码

  • 第二个有一个ListView和一个EditText - 来过滤listView

In my first screen, I want username EditText to have focus on startup and Keyboard should be visible ..这是我的实现(通过删除不必要/不相关的代码简化)..

app_login.xml

<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:paddingLeft="20dip"  
    android:paddingRight="20dip">

    <EditText android:id="@+id/username" android:singleLine="true" 
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content" android:hint="Username"  
        android:imeOptions="actionDone" android:inputType="text"
        android:maxLines="1"/>

    <EditText android:id="@+id/password" android:password="true" 
        android:singleLine="true"  
        android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:hint="Password" />

</LinearLayout>

AppLogin.java

class AppLogin extends Activity{
    private EditText mUserNameEdit = null;
    private EditText mPasswordEdit = null;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_login);

        mUserNameEdit  =    (EditText) findViewById(R.id.username);
        mPasswordEdit  =    (EditText) findViewById(R.id.password);

        /* code to show keyboard on startup.this code is not working.*/
        InputMethodManager imm =  (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(mUserNameEdit, InputMethodManager.SHOW_IMPLICIT);

    }//End of onCreate()
}

好吧, the keyboard is not showing at start up . 我的设计非常需要键盘 .

现在 second page ..我已经说过我有一个listView和EditText .. I want my keyboard to be hidden on start up only to appear when user touches the editText ..你能相信吗?不管怎么说 soft Keyboard is showing when I load the activity ..我无法隐藏它..

app_list_view.xml

<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical" >
    <EditText android:id="@+id/filter_edittext"       
        android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:hint="Search" android:inputType="text" android:maxLines="1"/>
    <ListView android:id="@id/android:list" android:layout_height="fill_parent"
        android:layout_weight="1.0" android:layout_width="fill_parent" 
        android:focusable="true" android:descendantFocusability="beforeDescendants"/>
</LinearLayout>

AppList.java

public class MyListActivity extends ListActivity{
   private EditText mfilterEditText;

    @Override
   public void onCreate(Bundle savedInstanceState) {        
      super.onCreate(savedInstanceState);
      setContentView(R.layout.app_list_view);

      mFilterEditText  =  (EditText) findViewById(R.id.filter_edittext);
      InputMethodManager imm = InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.hideSoftInputFromWindow(mFilterEditText.getWindowToken(), 0);
   }
}

简化

  • 在登录页面(第一页)我希望我的键盘在启动时可见..

  • 在SecondPage上我希望首先隐藏键盘,仅在用户触摸editText时显示

And my problem is I am getting exact opposite on both occasion ...希望有人在此之前遇到过这个问题 . 我正在模拟器和HTC Desire手机上进行测试..

最终结果

在这里所有朋友的帮助下,我得到了它的工作 .

1. To Show keyboard on startup

两个答案对我有用 . One provided by @CapDroid, which is to use a handler and post it delayed..

mUserNameEdit.postDelayed(new Runnable() {
  @Override
  public void run() {
    // TODO Auto-generated method stub
    InputMethodManager keyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    keyboard.showSoftInput(mUserNameEdit, 0);
  }
},50);

第二个答案是由@Dyarish提供的,事实上他与另一个SO线程相关联,这是我以前从未见过的 . 但有趣的是,这个解决方案是在我开始时引用的线程中给出的 . 而且我还没有尝试过,因为它在所有其他帖子都有很多选票的帖子中没有投票权 . 愚蠢的高度......

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

对我来说,第二个解决方案看起来很整洁,所以我决定坚持下去 . 但是第一个肯定有效 . 另外@Dyarish的答案包含一个聪明的黑客,使用EditText下面的ScrollView给EditText作为焦点..但我没有尝试过,但它应该工作 . 虽然不整齐..

2. To hide keyboard at activity start

只有一个答案适合我,由@Dyarish提供 . 解决方案是在xml中使用focusableInTouchMode设置作为包含editText的布局 . 这样做了

<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical" android:focusableInTouchMode="true">
    <EditText android:id="@+id/filter_edittext"       
        android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:hint="Search" android:inputType="text" android:maxLines="1"/>
    <ListView android:id="@id/android:list" android:layout_height="fill_parent"
        android:layout_weight="1.0" android:layout_width="fill_parent" 
        android:focusable="true" android:descendantFocusability="beforeDescendants"/>
</LinearLayout>

无论如何,在这两种情况下我最终都使用了Dyarish的答案 . 所以我正在向他颁发奖金 . 感谢所有其他试图帮助我的朋友 .

4 回答

  • 129

    将其添加到您的代码 android:focusableInTouchMode="true" 将确保您的编辑文本框启动时不会显示您的键盘 . 您希望将此行添加到包含EditTextBox的线性布局中 . 您应该能够使用它来解决您的问题 . 我测试了这个 . 简单解决方案

    即:在您的app_list_view.xml文件中

    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:orientation="vertical" 
        android:focusableInTouchMode="true">
        <EditText 
            android:id="@+id/filter_edittext"       
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:hint="Search" 
            android:inputType="text" 
            android:maxLines="1"/>
        <ListView 
            android:id="@id/android:list" 
            android:layout_height="fill_parent"
            android:layout_weight="1.0" 
            android:layout_width="fill_parent" 
            android:focusable="true" 
            android:descendantFocusability="beforeDescendants"/>
    </LinearLayout>
    

    ------------------ EDIT: To Make keyboard appear on startup -----------------------

    这是为了使他们的键盘在启动时出现在用户名edittextbox上 . 我所做的只是将一个空的Scrollview添加到.xml文件的底部,这会将第一个edittext置于焦点并弹出键盘 . 我承认这是一个黑客,但我假设你只是希望这个工作 . 我测试了它,它工作正常 .

    <?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="wrap_content" 
        android:paddingLeft="20dip"  
        android:paddingRight="20dip">
        <EditText 
            android:id="@+id/userName" 
            android:singleLine="true" 
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content" 
            android:hint="Username"  
            android:imeOptions="actionDone" 
            android:inputType="text"
            android:maxLines="1"
           />
        <EditText 
            android:id="@+id/password" 
            android:password="true" 
            android:singleLine="true"  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:hint="Password" />
        <ScrollView
            android:id="@+id/ScrollView01"  
            android:layout_height="fill_parent"   
            android:layout_width="fill_parent"> 
        </ScrollView>
    </LinearLayout>
    

    如果您正在寻找更有说服力的解决方案,我发现this问题可能对您有所帮助,它不像上面的解决方案那么简单,但可能是更好的解决方案 . 尽管如此,我还没有尝试过那些不起作用的东西 .

    希望这是你正在寻找的 .

    干杯!

  • 53

    UPDATE 2

    @Override
        protected void onResume() {
            super.onResume();
            mUserNameEdit.requestFocus();
    
            mUserNameEdit.postDelayed(new Runnable() {
    
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    InputMethodManager keyboard = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
                    keyboard.showSoftInput(mUserNameEdit, 0);
                }
            },200); //use 300 to make it run when coming back from lock screen
        }
    

    我非常努力地找到了解决方案...每当新的活动开始时键盘都无法打开但是我们可以在 onResume 中使用 Runnable 并且它工作正常所以请尝试此代码并检查...

    UPDATE 1

    AppLogin.java 中添加此行

    mUserNameEdit.requestFocus();
    

    AppList.java 中的这一行

    listview.requestFocus()'
    

    在此之后检查您的应用程序是否无效,然后在 AndroidManifest.xml 文件中添加此行

    <activity android:name=".AppLogin" android:configChanges="keyboardHidden|orientation"></activity>
    <activity android:name=".AppList" android:configChanges="keyboard|orientation"></activity>
    

    ORIGINAL ANSWER

    InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
    

    用于隐藏键盘

    imm.hideSoftInputFromWindow(ed.getWindowToken(), 0);
    

    用于显示键盘

    imm.showSoftInput(ed, 0);
    

    专注于EditText

    ed.requestFocus();
    

    其中ed是EditText

  • 160

    试试这个代码 .

    用于显示软键盘:

    InputMethodManager imm = (InputMethodManager)
                                     getSystemService(Context.INPUT_METHOD_SERVICE);
    if(imm != null){
            imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
        }
    

    隐藏SoftKeyboard -

    InputMethodManager imm = (InputMethodManager)
                                      getSystemService(Context.INPUT_METHOD_SERVICE);
    if(imm != null){
            imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
        }
    
  • 8

    你在第一个窗口尝试了 InputMethodManager.SHOW_IMPLICIT 吗?

    并隐藏在第二个窗口中使用 InputMethodManager.HIDE_IMPLICIT_ONLY

    EDIT :

    如果它还没有工作那么可能你把它放在错误的地方 . 覆盖 onFinishInflate() 并在那里显示/隐藏 .

    @override
    public void onFinishInflate() {
         /* code to show keyboard on startup */
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(mUserNameEdit, InputMethodManager.SHOW_IMPLICIT);
    }
    

相关问题