![](/img/trans.png)
[英]How can I focus on a collapsible action view EditText item in the action bar (when it is expanded) and force the soft keyboard to open?
[英]How to open an EditText View when clicking on an Action Bar Menu Options Item
我正在开发一个应用程序(在 Java 中),其中操作栏具有项目/选项的选项菜单(onCreateOptionsMenu 导致屏幕右上角的三个垂直点),因此当用户单击三个垂直点时,菜单展开,显示项目/选项列表。 我希望这些项目之一在单击时打开一个 EditText 视图以使用户能够输入一些文本。 我花了很多时间研究这个,但我一直无法找到它是如何完成的。 有人可以提供如何实现这一目标的大纲,或者给我举个例子。
非常感谢
创建一个dialog_layout.xml
文件并添加以下代码
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:id="@+id/editTextDialog"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/edit_text_background"
android:hint="@string/about_routine_dialog_image"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:textColor="#3C3B3B"
android:inputType="number"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end">
<Button
android:id="@+id/buttonDialogSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit"/>
</LinearLayout>
然后在用户单击该特定菜单项时在 function 调用showDialog
下面运行:
public void showDialog() {
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_layout);
final EditText editText = dialog.findViewById(R.id.editTextDialog);
Button buttonDialog = dialog.findViewById(R.id.buttonDialogSubmit);
buttonDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = editText.getText().toString(); //your text from editText
dialog.dismiss();
//do your work here
}
});
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.