Dod*_*odo 6 android button android-softkeyboard android-edittext
我的情况是:我有一个具有禁用焦点的EditText字段.在EditText字段旁边,我有两个输入法按钮.所以我想点击第一个按钮:打开软键盘并编辑EditText字段中的文本.我尝试了很多方法:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
Run Code Online (Sandbox Code Playgroud)
并不适合我.打开软键盘的唯一方法是:
toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
但是没有办法从EditText字段编辑信息.
您可以建议我在单击按钮时打开键盘并编辑某些EditText的文本.非常感谢!
编辑:
因此,EditText无法成为默认值.当我单击键盘按钮 - 应该是可聚焦的,然后显示软键盘输入文本并出现在EditText中.插入的其他方法是不需要键盘的ABC按钮.它将像摩尔斯电码输入 - 触摸并按住ABC按钮:)我将尝试在我的情况下实施的建议示例.感谢你们 :)
Dod*_*odo 16
谢谢你的帮助:)我使用了你给我的所有建议,搜索并测试了很多其他脚本,最后我的代码正在运行:)
这是我的最终代码:
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);
}
});
}
Run Code Online (Sandbox Code Playgroud)
它可能对某人有用:)谢谢!
and*_*ndr 12
不推荐您描述的设计.您违反了focusable
属性的目的,即不控制用户是否可以更改EditText
组件中的文本.
如果您计划提供替代输入方法,因为用例似乎需要这样(例如,您只允许在可编辑文本字段中使用某组符号),那么您可能应该在不允许用户的情况下完全禁用文本编辑更改字段的值.
声明您的可编辑字段:
<EditText
android:id="@+id/edit_text_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
Run Code Online (Sandbox Code Playgroud)
注意它的focusable
属性保留默认值.没关系,我们稍后会处理.声明一个将开始编辑过程的按钮:
<Button
android:id="@+id/button_show_ime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start editing" />
Run Code Online (Sandbox Code Playgroud)
现在,在你的Activity
声明中:
private EditText editText2;
private KeyListener originalKeyListener;
private Button buttonShowIme;
Run Code Online (Sandbox Code Playgroud)
在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);
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
我希望所有评论的代码都是不言自明的.在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();
}
});
Run Code Online (Sandbox Code Playgroud)