我想按下按钮“添加”时,隐藏键盘。屏幕上有两个
EditText
。键盘不会在开始活动时出现,这很好,但它不会在点击按钮时消失。
下面是堆栈溢出所有可能的问题,我已经看到其答案不帮我:
Close/hide the Android Soft Keyboard
Programmatically Hide/Show Android Soft Keyboard
How to hide Soft Keyboard when activity starts
How to hide soft keyboard on android after clicking outside EditText?
等等。
这里是我的代码:
AddActivity
public class AddActivity extends ActionBarActivity {
EditText text1,text2;
DbHelper db;
ListView l;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
db = new DbHelper(this);
l = (ListView) findViewById(R.id.listInAddActivity);
text1 = (EditText) findViewById(R.id.i1);
text2 = (EditText) findViewById(R.id.i2);
// text1.setInputType(InputType.TYPE_NULL);
// text2.setInputType(InputType.TYPE_NULL);
hideKeyboard();
loadDataInAdd();
}
public void addNewTask(View view) {
String s1 = text1.getText().toString();
String s2 = text2.getText().toString();
db.addData(s1,s2);
loadDataInAdd();
hideKeyboard();
}
public void loadDataInAdd()
{
try {
Cursor cursor = db.fetchData();
ListAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.tasks,
cursor,
new String[]{db._ID, db.COLUMN_1, db.COLUMN_2},
new int[]{R.id.idnum, R.id.c1, R.id.c2}, 0);
l.setAdapter(myAdapter);
}
catch(NullPointerException e)
{
e.printStackTrace();
}
// MainActivity.loadData();
}
private void hideKeyboard() {
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
大多数的答案是关于InputManager
方法,但在点击时,它不适合我的工作,即不隐藏键盘按钮。下面是我用的InputManager
另一个变化:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
我也试过这样:
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
);
,这不工作,要么(键盘是不是隐藏在点击按钮)。
我也曾尝试:
<activity android:name=".AddActivity"
android:label="@string/app_name"
android:parentActivityName=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden">
</activity>
和
<activity android:name=".AddActivity"
android:label="@string/app_name"
android:parentActivityName=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden">
</activity>
有了这一个,我的应用程序停止工作:
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
这一个完全隐藏键盘(键盘做即使点击了editText
也不会出现):
text1.setInputType(InputType.TYPE_NULL);
text2.setInputType(InputType.TYPE_NULL);
我感谢您的回答。但所有的答案已经可用。如果您在_Google_上进行了更好的搜索。但没关系,一切都在一起。 –
@MD:你可能是对的,因为我是Android的初学者,所有的部分答案让我困惑不已。在过去的几天里,我经历了数百个链接(大部分来自SO),并试图实现所有的答案。但他们只是不适合我的情况。例如,'InputMethodManager'是要走的路,几乎所有的答案都表明,但是你可以看到我的问题是如何失败的。我必须从这个答案拼凑出一部分,从这个答案的一部分,我自己逻辑的一部分,以及代码应该到哪里去,等等。 –
雅这就是为什么我写'赞赏你的答案'。 +1 up –