pre*_*bgg 32 keyboard android android-edittext android-alertdialog
我打开输入对话框的代码如下:
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Dialog Title");
alert.setMessage("Request information");
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.edittextautotextlayout, null);
final EditText inputBox = (EditText) textEntryView.findViewById(R.id.my_et_layout);
alert.setView(inputBox);
Run Code Online (Sandbox Code Playgroud)
这很好用,除了我必须在软键盘出现之前点击文本输入行.
按照这里给出的建议我尝试插入:
inputBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
alert.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
Run Code Online (Sandbox Code Playgroud)
但Eclipse对象"没有为AlertDialog.Builder类型定义方法getWindow()".
似乎setOnFocusChangeListener代码适用于AlertDialog对象,但不适用于AlertDialog.Builder.我应该如何修改我的代码以使软键盘自动显示.
Def*_*ged 78
只要您始终需要在对话框打开后立即显示键盘而不是一次特定的窗体小部件内部获得焦点(例如,如果对话框只显示EditText一个按钮),您可以执行以下操作:
AlertDialog alertToShow = alert.create();
alertToShow.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
alertToShow.show();
Run Code Online (Sandbox Code Playgroud)
而不是.show()立即调用您的构建器,而是可以调用.create()哪个允许您在将其显示到屏幕上之前对其进行一些额外的处理.
pre*_*bgg 15
这是对miannelle的回应.
选择菜单选项时,将调用以下方法:
private void addNote() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.textentryalertdialog);
dialog.setTitle("Add note");
TextView msgText = (TextView) dialog.findViewById(R.id.messagetext);
msgText.setText("Whatever prompt you want");
final EditText inputLine = (EditText) dialog.findViewById(R.id.my_edittext);
Button okButton = (Button) dialog.findViewById(R.id.OKButton);
okButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
dialog.dismiss();
// app specific code
}
});
Button cancelButton = (Button) dialog.findViewById(R.id.CancelButton);
cancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
dialog.dismiss();
}
});
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();
}
Run Code Online (Sandbox Code Playgroud)
textentryalertdialog.xml文件定义包含的线性布局
TextView android:id ="@ + id/messagetext"......
EditText android:id ="@ + id/my_edittext"......
按钮android:id ="@ + id/OKButton"......
按钮android:id ="@ + id/CancelButton"...
我希望这有帮助.
| 归档时间: |
|
| 查看次数: |
26943 次 |
| 最近记录: |