我想按下按鈕「添加」時,隱藏鍵盤。屏幕上有兩個
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 –