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

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

问题描述


可能重复:

关闭/隐藏Android软键盘

首先首先,我已经看到了这个主题。我尝试了那里给出的接受方法..但没有什么对我有用..

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.


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

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

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

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

<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



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

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

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

<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



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

简化


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

  2. On SecondPage I希望键盘首先被隐藏,只有当用户触摸editText时才显示

我的问题是我是在两种情况下完全相反 ...希望有人在此之前遇到此问题。我正在模拟器和HTC Desire手机上进行测试..

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

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

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

1。在启动时显示键盘

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

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

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

Second answers is provided by @Dyarish, In fact he linked to another SO thread, which I haven't 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);

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

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。在活动开始时隐藏键盘

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

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> 

无论如何,我最终都使用了Dyarish的答案。所以我正在给他奖励。谢谢所有其他朋友
试图帮助我..

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

推荐答案

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

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:在你的app_list_view.xml文件中

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> 

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

------------------ To Make keyboard appear on startup -----------------------

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

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.

希望这是你想要的。

干杯!

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

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