![](/img/trans.png)
[英]Android : Handle back button click when keyboard is open for EditText
[英]Android: How to open keyboard for editing EditText when click button?
我的情況是:我有一個具有禁用焦點的 EditText 字段。 在 EditText 字段旁邊,我有兩個用於輸入法的按鈕。 所以我想當點擊第一個按鈕時:打開軟鍵盤並在 EditText 字段中編輯文本。 我嘗試了很多方法:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
並且對我不起作用。 打開軟鍵盤的唯一方法是:
toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
但無法從 EditText 字段編輯信息。
您可以建議我如何在單擊按鈕時打開鍵盤並為某些 EditText 編輯文本。 非常感謝!
編輯:
因此,默認情況下 EditText 不可聚焦。 當我單擊鍵盤按鈕時 - 應該是可聚焦的,然后向我顯示軟鍵盤以輸入文本並出現在 EditText 中。 其他插入方法是不需要鍵盤的 ABC 按鈕。 它將類似於莫爾斯電碼輸入 - 觸摸並按住 ABC 按鈕 :) 我將嘗試在我的情況下實施建議的示例。 感謝大伙們 :)
謝謝你們的幫助 :) 我使用了你給我的所有建議,搜索並測試了很多其他腳本,最后我的代碼可以工作了 :)
這是我的最終代碼:
InputEditText = (EditText) findViewById(R.id.InputText);
public void InputData() {
/* Keyboard Button Action */
KeyboardButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v(TAG, "On Keyboard Button click event!");
InputEditText.requestFocus();
InputEditText.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(InputEditText, InputMethodManager.SHOW_FORCED);
}
});
}
它可能對某人有用:) 謝謝!
不推薦您描述的設計。 您違反了focusable
屬性的目的,即不控制用戶是否可以更改EditText
組件中的文本。
如果您計划提供替代輸入法,因為用例似乎需要這樣做(例如,您只允許可編輯文本字段中的某些符號集),那么您應該在不允許用戶的時候完全禁用文本編輯更改字段的值。
聲明您的可編輯字段:
<EditText
android:id="@+id/edit_text_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
請注意,它的focusable
屬性保留了默認值。 沒關系,我們稍后會處理。 聲明一個將開始編輯過程的按鈕:
<Button
android:id="@+id/button_show_ime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start editing" />
現在,在您的Activity
聲明:
private EditText editText2;
private KeyListener originalKeyListener;
private Button buttonShowIme;
在onCreate()
這樣做:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ime_activity);
// Find out our editable field.
editText2 = (EditText)findViewById(R.id.edit_text_2);
// Save its key listener which makes it editable.
originalKeyListener = editText2.getKeyListener();
// Set it to null - this will make the field non-editable
editText2.setKeyListener(null);
// Find the button which will start editing process.
buttonShowIme = (Button)findViewById(R.id.button_show_ime);
// Attach an on-click listener.
buttonShowIme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Restore key listener - this will make the field editable again.
editText2.setKeyListener(originalKeyListener);
// Focus the field.
editText2.requestFocus();
// Show soft keyboard for the user to enter the value.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText2, InputMethodManager.SHOW_IMPLICIT);
}
});
// We also want to disable editing when the user exits the field.
// This will make the button the only non-programmatic way of editing it.
editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// If it loses focus...
if (!hasFocus) {
// Hide soft keyboard.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText2.getWindowToken(), 0);
// Make it non-editable again.
editText2.setKeyListener(null);
}
}
});
}
我希望帶有所有注釋的代碼是不言自明的。 在 API 8 和 17 上測試。
試試這個 :
final EditText myedit2 = (EditText) findViewById(R.id.myEditText2);
Button btsmall = (Button) findViewById(R.id.BtSmall);
btsmall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
myedit2.requestFocus();
}
});
我有使用此代碼在單擊按鈕時打開鍵盤。 像 。
btn1 = (Button) findViewById(R.id.btn1);
edt1 = (EditText) findViewById(R.id.edt1);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
edt1.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edt1, InputMethodManager.SHOW_IMPLICIT);
}
});
其完成的工作。
LinearLayout ll_about_me =(LinearLayout) view.findViewById(R.id.ll_about_me);
ll_about_me.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
mEtEmpAboutYou.requestFocus();
mEtEmpAboutYou.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEtEmpAboutYou, InputMethodManager.SHOW_FORCED);
return true;
}
});
對於那些使用片段的人,您可以這樣使用InputMethodManager
:
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
完整代碼:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
}
});
我希望這有幫助
EditText txt_categorie = findViewById(R.id.txt_categorie);
txt_categorie.requestFocus();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.