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);
}
}
简化
- 在登录页面(第一页),我希望我的键盘在启动时可见.
- 在第二页,我希望键盘先被隐藏,然后才出现
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的答案.所以我要把赏金授予他.感谢我所有的其他朋友 想要帮我的人.