【问题标题】:Listview set color for single item By position When the dialog box opensListview为单个项目设置颜色按位置对话框打开时
【发布时间】:2019-11-11 17:26:55
【问题描述】:

我尝试通过以下代码打开对话框时设置检查:

 private void openDilog() {
    final Dialog dialog = new Dialog(getActivity(), R.style.NewDialog);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.mylayout);
    dialog.setCancelable(false);
    mListView =dialog.findViewById(R.id.mylist);
    listItem = getResources().getStringArray(R.array.myarray);
    aAdapter = new ArrayAdapter(getActivity(), 
    android.R.layout.simple_list_item_1,android.R.id.text1, listItem);
    mListView.setAdapter(aAdapter);
    dialog.show();
    mListView.setItemChecked(1,true);
  }

我的xml布局mylayout:

   <LinearLayout
      ......>
        <ListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:choiceMode="singleChoice"
        android:listSelector="#ffffff"
        />
</LinearLayout>

我试图将支票放在位置 1,但它没有

我想改变背景颜色或强制选择项

请注意,此对话框在框架内

【问题讨论】:

  • 这里没有安卓开发者:(

标签: java android android-listview


【解决方案1】:

我相信您想要的是创建一个 AlertDialog 并在该对话框上使用 setSingleChoiceItems 方法:

private void openDialog() {
    Context context = getActivity();
    if (context == null) return;
    AlertDialog.Builder adb = new AlertDialog.Builder(context);
    adb.setSingleChoiceItems(R.array.myarray, 1, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    adb.show();
}

注意:这将自动选择位置 1 处的项目(列表中的第 2 项基于从索引 0 开始)

【讨论】:

  • 谢谢你的回答..现在没有“listview”了,你通过从“listview”切换到“radioButton”改变了设计风格,我没有问题但是我怎么把它放在里面对话框的设计..我希望看到我的对话框代码我如何将我的代码放入特殊样式中
  • 如果您需要比标准 AlertDialog 更自定义的内容,可以使用adb.setView(someView)。您可以自行设置带有 onClickListener 的适配器
  • 我对这一行有错误 ` final AlertDialog.Builder adb = new AlertDialog.Builder(context,R.style.NewDialog); adb.requestWindowFeature(Window.FEATURE_NO_TITLE); adb.setContentView(R.layout.s_name);`
  • 和 setView 不能使用 api 15 仅 21 及更高版本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-10
  • 2014-06-15
  • 1970-01-01
  • 2015-04-19
  • 2017-01-12
相关资源
最近更新 更多