android studio单选对话框确认后在底部浮动显示该选项内容
时间: 2023-05-30 14:05:15 浏览: 211
要实现这个功能,可以使用Android Studio的AlertDialog和ListView。首先,创建一个字符串数组来存储单选对话框中的选项内容。然后,在AlertDialog中使用setSingleChoiceItems方法将选项显示在ListView中。接下来,在AlertDialog的setPositiveButton方法中添加代码来获取所选选项的索引,并将其内容显示在底部的浮动文本中。
以下是示例代码:
```
final String[] options = {"Option 1", "Option 2", "Option 3"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose an option");
builder.setSingleChoiceItems(options, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int selectedPosition = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
if (selectedPosition != -1) {
String selectedOption = options[selectedPosition];
// display selected option in floating text view
TextView floatingTextView = findViewById(R.id.floating_text_view);
floatingTextView.setText(selectedOption);
floatingTextView.setVisibility(View.VISIBLE);
}
}
});
builder.setNegativeButton("Cancel", null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
```
在这个例子中,我们在AlertDialog的setPositiveButton方法中获取ListView中被选中的选项的索引。如果用户选中了一个选项,我们将该选项的内容显示在底部的浮动文本视图中。这里使用了一个TextView来实现浮动文本视图,但你可以使用任何你想要的视图。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.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)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)