Android - 以编程方式隐藏/显示软键盘 [英] Android - Programmatically Hide/Show Soft Keyboard

查看:38
本文介绍了Android - 以编程方式隐藏/显示软键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

可能的重复:
如何以编程方式关闭/隐藏 Android 软键盘?

首先我已经看到了这个主题.我尝试了那里给出的可接受的方法,但没有任何效果.

我的应用中有两个屏幕.

  • 第一个有 2 个 EditText - 一个用于用户名,一个用于密码
  • 第二个有一个 ListView 和一个 EditText - 用于过滤列表视图

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

#app_login.xml

<EditText android:id="@+id/username";机器人:singleLine =真";android:layout_width=fill_parent"android:layout_height="wrap_content";android:hint="用户名"android:imeOptions="actionDone";android:inputType="文本"android:maxLines=1"/><EditText android:id="@+id/password";机器人:密码=真"机器人:singleLine =真";android:layout_width=fill_parent"android:layout_height="wrap_content";android:hint="密码"/></LinearLayout>

#AppLogin.java

class AppLogin 扩展 Activity{private EditText mUserNameEdit = null;私人 EditText mPasswordEdit = null;@覆盖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);/* 启动时显示键盘的代码.此代码不起作用.*/InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);imm.showSoftInput(mUserNameEdit, InputMethodManager.SHOW_IMPLICIT);}//onCreate()结束}

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

现在进入第二页.正如我已经提到的,我在那里有一个 listView 和 EditText.我希望我的键盘在启动时隐藏,只在用户触摸 editText 时显示.你相信吗?我尝试的任何内容加载活动时显示的软键盘.我无法隐藏它.

#app_list_view.xml

<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{私人 EditText mfilterEditText;@覆盖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);}}

为了简化

  1. 在登录页面(第一页)我希望我的键盘在启动时可见.
  2. 在 SecondPage 上,我希望键盘先隐藏,然后才出现当用户触摸 editText

我的问题是我在这两种情况下都得到了完全相反的结果.希望有人以前遇到过这个问题.顺便说一句,我正在模拟器和 HTC Desire 手机上进行测试.

#最终结果

好吧,在我所有朋友的帮助下,我成功了.

1.在启动时显示键盘

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

mUserNameEdit.postDelayed(new Runnable() {@覆盖公共无效运行(){//TODO 自动生成的方法存根InputMethodManager 键盘 = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);键盘.showSoftInput(mUserNameEdit, 0);}},50);

第二个答案由@Dyarish 提供,事实上,他链接到另一个 SOF 线程,这是我以前从未见过的.但有趣的是,这个解决方案是在我一开始引用的线程中给出的.而且我没试过因为它在所有其他帖子都有大量选票的线程中获得零票.愚蠢至极.

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

对我来说,第二个解决方案看起来很整洁,所以我决定坚持下去......但第一个肯定有效.此外,@Dyarish 的答案包含使用 EditText 下方的 ScrollView 来为 EditText 提供焦点的巧妙技巧.不过我没试过,应该可以的.虽然不整洁.

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

只有一个答案对我有用,由@Dyarish 提供.解决方案是使用包含 EditText 的布局的 XML 中的 focusableInTouchMode 设置.这成功了

<EditText android:id="@+id/filter_edittext";android:layout_width=fill_parent"android:layout_height="wrap_content";android:hint="搜索"android:inputType="文本"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 的答案.所以我要给他赏金.感谢我所有的其他朋友谁想帮我.

解决方案

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

即:在您的 app_list_view.xml 文件中

<编辑文本android:id="@+id/filter_edittext"android:layout_width="fill_parent"android:layout_height="wrap_content"机器人:提示=搜索"机器人:输入类型=文本"android:maxLines="1"/><列表视图android:id="@id/android:list"android:layout_height="fill_parent"android:layout_weight="1.0"android:layout_width="fill_parent"机器人:焦点=真"android:descendantFocusability="beforeDescendants"/></LinearLayout>

------------------- 使键盘在启动时出现 -----------------------

这是为了让他们的键盘在启动时出现在用户名编辑文本框中.我所做的只是在 .xml 文件的底部添加了一个空的 Scrollview,这将使第一个编辑文本成为焦点并弹出键盘.我承认这是一个黑客,但我假设你只是想让它工作.我已经测试过了,它工作正常.

<编辑文本android:id="@+id/用户名"机器人:单线=真"android:layout_width="fill_parent"android:layout_height="wrap_content"机器人:提示=用户名"android:imeOptions="actionDone"机器人:输入类型=文本"机器人:maxLines =1"/><编辑文本android:id="@+id/密码"机器人:密码=真"机器人:单线=真"android:layout_width="fill_parent"android:layout_height="wrap_content"android:hint="密码"/><滚动视图android:id="@+id/ScrollView01"android:layout_height="fill_parent"android:layout_width="fill_parent"></ScrollView></LinearLayout>

如果您正在寻找更有说服力的解决方案,我已经找到了 this 可能对您有所帮助的问题,它不像上面的解决方案那么简单,但可能是更好的解决方案.我还没有测试过,但它显然有效.我认为它类似于您尝试过的解决方案,但对您不起作用.

希望这是您正在寻找的.

干杯!

Possible Duplicate:
How do you close/hide the Android soft keyboard programmatically?

First thing first I already saw this thread. I tried the accepted methods given there, but nothing worked for me.

I have two screens in my app.

  • First one has 2 EditText - One for username and one for password
  • Second one have one ListView, and an EditText - to filter the listView

In my first screen, I want username EditText to have focus on startup and the Keyboard should be visible. This is my implementation (simplified by removing unnecessary/unrelated code).

#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()
}

Well, the keyboard is not showing at startup. And my design badly requires a keyboard there.

Now on to second page. As I already mentioned, I have a listView and EditText there. I want my keyboard to be hidden on startup only to appear when the user touches the editText. Can you believe it? whatever I tried soft Keyboard is showing when I load the activity. I am not able to hide it.

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

To simplify

  1. On Login Page (first Page) I want my keyboard to be visible on startup.
  2. On SecondPage I want the keyboard to be hidden first, only to appear when the user touches editText

And my problem is I am getting the exact opposite on both occasions. Hope someone faced this issue before. BTW I am testing on the simulator and HTC Desire phone.

#FINAL OUTCOME

Well, I got it working, with the help of all my friends here.

1. To Show keyboard on startup

Two answers worked for me. 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);

The second answer is provided by @Dyarish, In fact, he linked to another SOF thread, which I haven't seen before. But the funny thing is that this solution is given in the thread which I referenced at the start. And I haven't tried it out because it had zero votes in a thread where all other posts have plenty of votes. Height of foolishness.

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

For me, the second solution looked neat, so I decided to stick with it..But the first one certainly works. Also, @Dyarish's answer contains a clever hack of using a ScrollView below EditText to give EditText the focus. But I haven't tried it, but it should work. Not neat though.

2. To hide keyboard at activity start

Only one answer worked for me, which is provided by @Dyarish. And the solution is to use focusableInTouchMode settings in XML for the layout containing the EditTexts. This did the trick

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

Anyway, I end up using Dyarish's answer in both cases. So I am awarding the bounty to him. Thanks to all my other friends who tried to help me.

解决方案

Adding this to your code android:focusableInTouchMode="true" will make sure that your keypad doesn't appear on startup for your edittext box. You want to add this line to your linear layout that contains the EditTextBox. You should be able to play with this to solve both your problems. I have tested this. Simple solution.

ie: In your app_list_view.xml file

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

This is to make they Keyboard appear on the username edittextbox on startup. All I've done is added an empty Scrollview to the bottom of the .xml file, this puts the first edittext into focus and pops up the keyboard. I admit this is a hack, but I am assuming you just want this to work. I've tested it, and it works fine.

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

If you are looking for a more eloquent solution, I've found this question which might help you out, it is not as simple as the solution above but probably a better solution. I haven't tested it but it apparently works. I think it is similar to the solution you've tried which didn't work for you though.

Hope this is what you are looking for.

Cheers!

这篇关于Android - 以编程方式隐藏/显示软键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆