问题
可能重复:关闭/隐藏Android软键盘
首先我已经看过thisthread。我试过那里接受的方法..但没有什么对我有用..
我的应用程序中有两个屏幕。
- 第一个有2个EditText - 一个用于用户名,一个用于密码
- 第二个有一个ListView和一个EditText - 来过滤listView
在我的第一个屏幕中,我希望用户名EditText专注于启动,键盘应该是可见的..这是我的实现(通过删除不必要/不相关的代码简化)..
#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()
}
好吧,键盘没有显示在启动.而我的设计非常需要键盘那里..
现在在第二页..我已经说过我有一个listView和EditText那里.我希望我的键盘在启动时隐藏,只有当用户触摸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>
#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);
}
}
简化
- 在登录页面(第一页)我希望我的键盘在启动时可见..
- 在SecondPage上我希望首先隐藏键盘,仅在用户触摸editText时显示
我的问题是我在两个场合正好相反...希望有人在此之前遇到过这个问题。我正在模拟器和HTC Desire手机上进行测试..
#FINAL OUTCOME
在这里所有朋友的帮助下,我得到了它的工作。
1.在启动时显示键盘
两个答案对我有用.一个由@CapDroid提供,即使用处理程序并将其发布延迟..
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线程相关联,这是我以前从未见过的。但有趣的是,这个解决方案是在我开始时引用的线程中给出的。而且我还没有尝试过,因为它在所有其他帖子都有很多选票的帖子中没有投票权。愚蠢的高度......
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
对我来说,第二个解决方案看起来很整洁,所以我决定坚持下去。但是第一个肯定有效。另外@Dyarish的答案包含一个聪明的黑客,使用EditText下面的ScrollView给EditText作为焦点..但我没有尝试过,但它应该工作。虽然不整齐..
2.在活动开始时隐藏键盘
只有一个答案适合我,由@Dyarish提供。解决方案是在xml中使用focusableInTouchMode设置作为包含editText的布局。这样做了
<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的答案。所以我正在向他颁发奖金。感谢所有其他试图帮助我的朋友。
#1 热门回答(158 赞)
更新2
@Override
protected void onResume() {
super.onResume();
mUserNameEdit.requestFocus();
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);
}
},200); //use 300 to make it run when coming back from lock screen
}
我非常努力地找到了一个解决方案...每当一个新的活动开始然后键盘打开但我们可以使用RunnableinonResume并且它工作正常所以请尝试此代码并检查...
更新1
在AppLogin.java
中添加此行
mUserNameEdit.requestFocus();
这一行在你的AppList.java
中
listview.requestFocus()'
在此之后检查你的应用程序是否无效,然后在你的AndroidManifest.xml
文件中添加此行
<activity android:name=".AppLogin" android:configChanges="keyboardHidden|orientation"></activity>
<activity android:name=".AppList" android:configChanges="keyboard|orientation"></activity>
ORIGINAL ANSWER
InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
用于隐藏键盘
imm.hideSoftInputFromWindow(ed.getWindowToken(), 0);
用于显示键盘
imm.showSoftInput(ed, 0);
专注于EditText
ed.requestFocus();
其中ed是EditText
#2 热门回答(123 赞)
将此添加到你的代码android:focusableInTouchMode="true"
将确保你的编辑文本框启动时不显示你的键盘。你希望将此行添加到包含EditTextBox的线性布局中。你应该能够使用它来解决你的问题。我测试了这个。简单解决方案
即:在你的app_list_view.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>
------------------编辑:使键盘出现在启动时---------------------- -
这是为了使他们的键盘在启动时出现在用户名edittextbox上。我所做的只是将一个空的Scrollview添加到.xml文件的底部,这会将第一个edittext置于焦点并弹出键盘。我承认这是一个黑客,但我假设你只是希望这个工作。我测试了它,它工作正常。
<?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问题可能对你有帮助,它不像上面的解决方案那么简单,但可能是更好的解决方案。我没有测试它,但它显然有效。我认为它类似于你尝试过的解决方案,虽然不适用于你。
希望这是你正在寻找的。
干杯!
#3 热门回答(52 赞)
试试这个代码。
用于显示软键盘:
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
}
隐藏SoftKeyboard -
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}