android 写一个带一个输入框的对话框
时间: 2023-12-12 21:04:34 浏览: 33
可以使用 Android 自带的 AlertDialog 和 EditText 组件来实现带一个输入框的对话框。
以下是一个简单的实现示例:
1. 在布局文件中定义一个 EditText 组件。
```
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
```
2. 在代码中创建一个 AlertDialog 对象,将 EditText 添加到对话框中。
```
// 创建 AlertDialog.Builder 对象
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// 设置对话框标题和消息
builder.setTitle("输入框对话框");
builder.setMessage("请输入内容:");
// 添加 EditText 组件到对话框中
final EditText editText = new EditText(this);
builder.setView(editText);
// 设置对话框按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 处理确定按钮点击事件
String text = editText.getText().toString();
Toast.makeText(MainActivity.this, "输入内容为:" + text, Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", null);
// 创建并显示对话框
AlertDialog dialog = builder.create();
dialog.show();
```
以上代码将创建一个带一个输入框的对话框,并在用户点击确定按钮时获取输入框中的内容并显示在 Toast 中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)