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

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

问题描述

  

可能重复:
  如何关闭/隐藏Android的软键盘?

第一件事,第一,我已经看到了<一个href="http://stackoverflow.com/questions/1109022/how-to-close-hide-the-android-soft-keyboard">this线。我试着接受的方法给出there..But没有什么工作对我来说..

我在我的应用程序的两个屏幕。

  • 第一招有2个EditText上的 - 一个用户名和一个密码
  • 第二1具有一个的ListView,和一个EditText - 过滤 ListView控件

在我的第一个画面,我想用户名的EditText有重点启动和键盘应该是可见。这是我的执行(简化通过删除不必要/不相关的codeS)..

app_login.xml

 &LT;的LinearLayout机器人:方向=垂直机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT机器人:以下属性来=20dip
    机器人:paddingRight =20dip&GT;

    &LT; EditText上机器人:ID =@ + ID /用户名机器人:单线=真
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT机器人:提示=用户名
        机器人:imeOptions =actionDone机器人:inputType =文本
        机器人:MAXLINES =1/&GT;

    &LT; EditText上机器人:ID =@ + ID /密码机器人:密码=真
        机器人:单线=真
        机器人:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT
        机器人:提示=密码/&GT;

&LT; / LinearLayout中&GT;
 

AppLogin.java

 类AppLogin延伸活动{
    私人的EditText mUserNameEdit = NULL;
    私人的EditText mPasswordEdit = NULL;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.app_login);

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

        / * code,显示键盘上startup.this code不工作。* /
        InputMethodManager IMM =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(mUserNameEdit,InputMethodManager.SHOW_IMPLICIT);

    } //的onCreate结束()
}
 

嗯,键盘没有显示在启动。而我的设计非常需要一个键盘有..

现在就为第二页。我已经说了我有一个ListView和EditText上有.. 我想我的键盘被隐藏在启动时只有当用户触摸EDITTEXT出现 ..你能相信吗?无论我试过软键盘显示了当我加载活动 ..我不能把它藏起来。

app_list_view.xml

 &LT; LinearLayout中的android:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT
    机器人:方向=垂直&GT;
    &LT; EditText上机器人:ID =@ + ID / filter_edittext
        机器人:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT
        机器人:提示=搜索安卓inputType =文本的Andr​​oid版本:MAXLINES =1/&GT;
    &LT; ListView的机器人:ID =@ ID / Android的:清单机器人:layout_height =FILL_PARENT
        机器人:layout_weight =1.0的Andr​​oid:layout_width =FILL_PARENT
        机器人:可聚焦=真正的机器人:descendantFocusability =beforeDescendants/&GT;
&LT; / LinearLayout中&GT;
 

AppList.java

 公共类MyListActivity扩展ListActivity {
   私人的EditText mfilterEditText;

    @覆盖
   公共无效的onCreate(包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的手机测试..这个问题before..BTW

最后的结局

嗯,我懂了工作,与这里的所有朋友们的帮助。

1。要查看键盘上启动

两个答案为我工作。 一个接@CapDroid,这是使用一个处理程序,并张贴延迟。

提供

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

二答案是@Dyarish提供,事实上他链接到其他SO线程,这是我以前没有见过。但 有趣的是,这个解决方案中给出了我在开始提到的线程。而且我还没有试过 它,因为它有零票线程,所有其他职位有大量愚蠢的votes..Height的。

<$p$p><$c$c>getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

对于我来说,第二个解决方案显得整洁,所以我决定坚持it..But第一个肯定的作品。 同时@ Dyarish的回答包含使用下面的EditText滚动型给EditText上我还没有尝试过的focus..But的一个聪明的黑客,但它应该工作。不整洁,但..

2。若要隐藏键盘,在活动开始

答案只有一个工作对我来说,这是由@Dyarish提供。和解决方案是使用 以xml focusableInTouchMode设置用于容纳EDITTEXT的布局。这样做有

 &LT; LinearLayout中的android:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT
    机器人:方向=垂直机器人:focusableInTouchMode =真正的&GT;
    &LT; EditText上机器人:ID =@ + ID / filter_edittext
        机器人:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT
        机器人:提示=搜索安卓inputType =文本的Andr​​oid版本:MAXLINES =1/&GT;
    &LT; ListView的机器人:ID =@ ID / Android的:清单机器人:layout_height =FILL_PARENT
        机器人:layout_weight =1.0的Andr​​oid:layout_width =FILL_PARENT
        机器人:可聚焦=真正的机器人:descendantFocusability =beforeDescendants/&GT;
&LT; / LinearLayout中&GT;
 

反正我最终使用Dyarish的回答在两种情况下。所以我授予boundy到him..Thanks所有的朋友 谁试图帮助我..

解决方案

添加到您code 安卓focusableInTouchMode =真正的将确保您的键盘没有出现在启动时为您的EditText框。要将此行添加到您的包含EditTextBox线性布局。您应该能够与此发挥既解决你的问题。我测试了这一点。简单的解决方案。

例如:在你的app_list_view.xml文件

 &LT;的LinearLayout
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直
    机器人:focusableInTouchMode =真正的&GT;
    &LT;的EditText
        机器人:ID =@ + ID / filter_edittext
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:提示=搜索
        机器人:inputType =文本
        机器人:MAXLINES =1/&GT;
    &LT;的ListView
        机器人:ID =@ ID / Android的:清单
        机器人:layout_height =FILL_PARENT
        机器人:layout_weight =1.0
        机器人:layout_width =FILL_PARENT
        机器人:可聚焦=真
        机器人:descendantFocusability =beforeDescendants/&GT;
&LT; / LinearLayout中&GT;
 

------------------的编辑:使在启动时键盘会出现 -------------- ---------

这是使他们的键盘出现在启动时的用户名edittextbox。所有我所做的就是添加一个空的滚动型到.xml文件的底部,这使第一的EditText成为关注的焦点,并弹出键盘。我承认这是一个黑客,但我假设你只是想这个工作。我测试过它,它工作正常。

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:以下属性来=20dip
    机器人:paddingRight =20dip&GT;
    &LT;的EditText
        机器人:ID =@ + ID /用户名
        机器人:单线=真
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:提示=用户名
        机器人:imeOptions =actionDone
        机器人:inputType =文本
        机器人:MAXLINES =1
       /&GT;
    &LT;的EditText
        机器人:ID =@ + ID /密码
        机器人:密码=真
        机器人:单线=真
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:提示=密码/&GT;
    &LT;滚动型
        机器人:ID =@ + ID / ScrollView01
        机器人:layout_height =FILL_PARENT
        机器人:layout_width =FILL_PARENT&GT;
    &LT; /滚动型&GT;
&LT; / LinearLayout中&GT;
 

如果你正在寻找一个更雄辩的解决方案,我发现<一href="http://stackoverflow.com/questions/6100791/how-to-automatically-show-soft-keyboard-on-start">this问题这可能会帮助你,它不是那么简单,上面的解决方案,但可能是一个更好的解决方案。我没有测试它,但它显然是工作。我认为这是类似于这并不会为你工作,虽然你已经尝试过的解决方案。

希望这是你所期待的。

干杯!

Possible Duplicate:
How to close/hide the Android Soft Keyboard?

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

I have two screens in my app.

  • First one have 2 EditText's - 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 Keyboard should be visible..This is my implementation (simplified by removing unnecessary/unrelated codes)..

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 start up. And my design badly require a keyboard there..

Now on to second page..I already said I have a listView and EditText there..I want my keyboard to be hidden on start up only to appear when 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 start up..
  2. On SecondPage I want keyboard to be hidden first, only to appear when user touches editText

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

FINAL OUTCOME

Well I got it working, with the help of all 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);

Second answers is provided by @Dyarish, Infact he linked to another SO thread, which I havent seen before. But the funny thing is that, this solution is given in the thread which I referenced at 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 first one certainly works. Also @Dyarish's answer contain 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 editText's. 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 boundy to him..Thanks all 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天全站免登陆