[英]EditText - keyboard doesn't appear on focus
我有這個煩人的問題。 我的應用程序有2個活動(標簽):Activity1:listview,Activity2:editText + listview。 應用以Tab1(Activity1)開頭。 當我打開第二個活動(帶有edittext)時,無論是否選擇EditText(可編程),當我單擊EditText時,都不會發生任何反應(應該出現softKeyboard)。 唯一的解決方案是更改活動(單擊Tab1小部件)並返回活動2-在此選項卡交換之后,鍵盤可以正常工作。
具有edittext的XML布局的一部分:
<EditText
android:hint="Wyszukaj..."
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:inputType="textAutoComplete|text"
android:singleLine="true"
android:focusable="true"
android:focusableInTouchMode="true"
>
這是來自Activity2的2個替代方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2);
this.db = DataBase.getInstance();
this.ds = DataSource.getInstance();
this.prepareListView();
}
@Override
protected void onResume() {
super.onResume();
this.doubleBackToExitPressedOnce = false;
}
private void prepareListView() {
sbal = this.db.getAllStops();
adapter = new StopListAdapter(this, sbal);
lv = (ListView) findViewById(R.id.tab2list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(onClick);
EditText et = (EditText) findViewById(R.id.editText1);
et.addTextChangedListener(changeWatcher);
registerForContextMenu(lv);
}
您有什么想法,在這種情況下XMLcode和活動代碼應該是什么樣子?
解決方法是,顯式調用onCreate()和onResume()方法上的軟鍵盤。
editText.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (editText.isEnabled() && editText.isFocusable()) {
editText.post(new Runnable() {
@Override
public void run() {
Context context = getApplicationContext();
final InputMethodManager imm =
(InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editor,InputMethodManager.SHOW_IMPLICIT);
}
});
}
}
});
希望這可以幫助 :)
嘗試從xml中刪除關於“ focusable”的兩行內容。 我有一些非常相似的東西,沒有它們也可以工作
try {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
YourEditText.getWindowToken(), 0);
} catch (Exception e) {
e.printStackTrace();
}
嘗試使用edittext ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.