以编程方式隐藏/显示Android软键盘

Kri*_*dra 233 java android show hide soft-keyboard

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

首先,我已经看到了这个帖子.我试过那里接受的方法..但没有什么对我有用..

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

  • 第一个有2个EditText - 一个用于用户名,一个用于密码
  • 第二个有一个ListView和一个EditText - 来过滤listView

在我的第一个屏幕中,我希望用户名EditText专注于启动,键盘应该是可见的 ..这是我的实现(通过删除不必要/不相关的代码简化)..

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>
Run Code Online (Sandbox Code Playgroud)

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()
}
Run Code Online (Sandbox Code Playgroud)

好吧,键盘在启动时没有显示.我的设计非常需要键盘.

现在转到第二页 .. 已经说过我有一个listView和EditText那里.. 我希望我的键盘在启动时隐藏,只有在用户触摸editText时出现 ..你能相信吗?无论我尝试什么软键盘在我加载活动显示 ..我无法隐藏它..

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>     
Run Code Online (Sandbox Code Playgroud)

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);
   }
}
Run Code Online (Sandbox Code Playgroud)

简化

  1. 在登录页面(第一页)我希望我的键盘在启动时可见..
  2. 在SecondPage上,我希望首先隐藏键盘,仅在用户触摸editText时显示

而且我的问题是我两次都得到了相反的结果 ......希望有人在此之前遇到过这个问题.我正在模拟器和HTC Desire手机上进行测试..

最后的结果

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

1.在启动时显示键盘

两个答案对我有用.一个由@CapDroid提供,即使用处理程序并将其发布延迟..

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);
Run Code Online (Sandbox Code Playgroud)

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

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Run Code Online (Sandbox Code Playgroud)

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

2.在活动开始时隐藏键盘

只有一个答案适合我,由@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> 
Run Code Online (Sandbox Code Playgroud)

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

Nir*_*tel 160

更新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
    }
Run Code Online (Sandbox Code Playgroud)

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

更新1

在你的.中添加这一行 AppLogin.java

mUserNameEdit.requestFocus();
Run Code Online (Sandbox Code Playgroud)

在你的这一行 AppList.java

listview.requestFocus()'
Run Code Online (Sandbox Code Playgroud)

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

<activity android:name=".AppLogin" android:configChanges="keyboardHidden|orientation"></activity>
<activity android:name=".AppList" android:configChanges="keyboard|orientation"></activity>
Run Code Online (Sandbox Code Playgroud)

原始答案

 InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
Run Code Online (Sandbox Code Playgroud)

用于隐藏键盘

 imm.hideSoftInputFromWindow(ed.getWindowToken(), 0); 
Run Code Online (Sandbox Code Playgroud)

用于显示键盘

 imm.showSoftInput(ed, 0);
Run Code Online (Sandbox Code Playgroud)

专注于EditText

 ed.requestFocus();
Run Code Online (Sandbox Code Playgroud)

其中ed是EditText

  • postDelayed解决方案也适用于我.+1 (5认同)
  • @sHaH好这是我在问题中链接的主题的答案,对吧?唯一的区别是你试过Service.INPUT_METHOD_SERVICE而不是我试过的Context.INPUT_METHOD_SERVICE .. (2认同)
  • nope..Not working..Getting沮丧.. :( (2认同)

Dav*_*ave 130

将其添加到您的代码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> 
Run Code Online (Sandbox Code Playgroud)

------------------ 编辑:在启动时显示键盘 -----------------------

这是为了使他们的键盘在启动时出现在用户名edittextbox上.我所做的就是在.xml文件的底部添加一个空的Scrollview,这会将第一个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>
Run Code Online (Sandbox Code Playgroud)

如果您正在寻找更有说服力的解决方案,我发现这个问题可能对您有所帮助,它不像上面的解决方案那么简单,但可能是更好的解决方案.我没有测试它,但它显然有效.我认为它类似于你尝试过的解决方案,虽然不适合你.

希望这是你正在寻找的.

干杯!


Lal*_*ani 54

试试这个代码.

用于显示软键盘:

InputMethodManager imm = (InputMethodManager)
                                 getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
    }
Run Code Online (Sandbox Code Playgroud)

隐藏SoftKeyboard -

InputMethodManager imm = (InputMethodManager)
                                  getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
        imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }
Run Code Online (Sandbox Code Playgroud)

  • 必须是[imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,0);]才能显示[imm.toggleSoftInput(0,InputMethodManager.HIDE_IMPLICIT_ONLY);]以便隐藏. (2认同)

Ron*_*nie 8

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

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

编辑:

如果它仍然没有工作,那么可能你把它放在错误的地方.覆盖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);
}
Run Code Online (Sandbox Code Playgroud)