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

第一件事,我已经看了this条了.我try 了那里给出的公认的方法,但对我来说都不管用.

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

  • 第一个有两个EditText——一个是用户名,一个是密码
  • 第二个有一个ListView和一个EditText,用于过滤

In my first screen, I want username EditText to have focus on startup and the 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>

#阿普洛金.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 startup.我的设计非常需要一个键盘.

现在转到second page.正如我已经提到的,我有一个listView和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>     

#阿普利斯特.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. 在第二页,我希望键盘先被隐藏,然后才出现

And my problem is I am getting the exact opposite on both occasions.希望以前有人遇到过这个问题.顺便说一句,我正在模拟器和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提供,事实上,他链接到了另一个SOF线程,这是我以前没有见过的.但有趣的是,这个解决方案是在我一开始引用的线程中给出的.我还没试过

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

对我来说,第二个解决方案看起来很好,所以我决定坚持下go ..但第一种方法确实有效.

2. To hide keyboard at activity start

只有一个答案对我有效,@Dyarish提供了这个答案.解决方法是使用

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

不管怎样,在这两个 case 中我最终都使用了Dyarish的答案.所以我要把赏金授予他.感谢我所有的其他朋友 想要帮我的人.

推荐答案

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

ie:在你的应用程序列表视图中.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 -----------------------

这是为了让它们在启动时出现在用户名编辑文本框中.我所做的就是在页面底部添加一个空的滚动视图.xml文件,这会将第一个编辑文本置于焦点,并弹出键盘.我承认这是黑客攻击,但我想你只是想让它起作用.我已经测试过了,效果很好.

<?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个问题,它可能会对你有所帮助,它不像上面的解决方案那么简单,但可能是一个更好的解决方案.我还没有测试过,但它显然是有效的.我想这和你试过的解决方案很相似,不过对你不起作用.

希望这就是你要找的东西.

干杯

Java相关问答推荐

如何用Java表示C++类以通过FFI使用?

如何在PFA中使用不确定的进度指标制作可暂停的任务?

填写文本字段后锁定PDF

多个Java线程和TreeMap.put()的非预期行为

弹簧靴和龙目岛

解析Javadoc时链接的全限定类名

如何打印本系列的第n项y=-(1)-(1+2)+(1+2+3)+(1+2+3+4)-(1+2+3+4+5)...Java中的(1+2+3+4...+n)

SpringBootreact 式Web应用程序的Spring Cloud Configer服务器中的资源控制器损坏

如何找到MongoDB文档并进行本地化?

如何使用Jackson将XML元素与值和属性一起封装

使用PDFBox从PDF中删除图像

基于接口的投影、原生查询和枚举

Java中将文本拆分为数字或十进制数字和字符串

S,要对Java复制构造函数深度克隆所有属性进行单元测试,最可靠的方法是什么?

删除打印语句会影响功能...腐败在起作用?

X=x*0.90;产生有损转换误差.X*=0.90;不是.为什么?

如何使这两种方法合二为一?

Win32函数的JNA绑定DwmGetColorizationColor返回E_INVALIDARG错误

获取所有可以处理Invent.ACTION_MEDIA_BUTTON Android 13 API33的Android包

如何使用带有可选参数的类生成器?