简体   繁体   English

android中的单选选择对话框

[英]Single choice selection dialog in android

I am making an android program. 我正在制作一个Android程序。 In my app, I am using a single choice selection AlertDialog whose items are added programmatically. 在我的应用程序中,我使用单选项AlertDialog,其项目以编程方式添加。 What I want to do is: 我想做的是:

  • set a background color to the selected item next time user opens the dialog, 下次用户打开对话框时,为所选项目设置背景颜色,
  • show selected item in the middle of the dialog(this is a problem because there are about 20 items). 在对话框中间显示所选项目(这是一个问题,因为大约有20个项目)。

Here is what I have:XML: 这就是我所拥有的:XML:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="5dp" >



    <Button
        android:id="@+id/selectDateButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Please, select date" />

</LinearLayout>

JAVA: JAVA:

public class ExperimentListView extends Activity {

private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
private static Calendar calendar = Calendar.getInstance();
private static Button selectDateButton;
private static String[] items;
private static int selectedDatePosition = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view_ex);

    selectDateButton = (Button)findViewById(R.id.selectDateButton);
    selectDateButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            items = new String[20];
            for (int i = 0; i < 20; i++) {
                items[i] = DATE_FORMAT.format(calendar.getTime());
                calendar.add(Calendar.DATE, 1);
            }
            showListView();
        }
    });

}

private void showListView() {
    AlertDialog.Builder builder = new AlertDialog.Builder(ExperimentListView.this);
    builder.setTitle("Select date");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            selectedDatePosition = which;
            selectDateButton.setText(items[selectedDatePosition]);
        }
    });
    AlertDialog alertDialog = builder.create();
    alertDialog.getListView().setSelection(selectedDatePosition);
    alertDialog.show();
 }
}

I haven't been able to find a solution to this so far and I would be grateful if someone can help. 到目前为止,我还没有找到解决方案,如果有人可以提供帮助,我将不胜感激。 Thanks in advance. 提前致谢。

Instead of using builder.setItem use builder.setSingleChoiceItems with selected item position passed as an argumet like 而不是使用builder.setItem使用builder.setSingleChoiceItems与选定的项目位置作为参数传递

    builder.setSingleChoiceItems(strArray, selected_pos, new DialogInterface.OnClickListener ()
    {
         @Override
         public void onClick(DialogInterface dialog, int which) 
         {

         }
    });

1)To set the background color you can use custom view for your Alert Dialog 1)要设置背景颜色,可以使用警报对话框的自定义视图

2)to show the selected item in the middle of the dialog you need to swap its position in the array which you are passing to list for alert dialog. 2)要在对话框中间显示所选项目,您需要要传递到列表中的数组中交换其位置以进行警告对话框。

3)And as you want background color respected to the item previously selected ,use shared preferences to store item and background,and regain the background and item selected when user open the alert dialog. 3)如果您希望背景颜色与先前选择的项目相符,请使用共享首选项来存储项目和背景,并在用户打开警报对话框时重新获得所选的背景和项目。

And as Deepika said use setSingleChoiceItems for single choice selection. 正如Deepika所说,使用setSingleChoiceItems进行单选选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM