• 弹出对话框中的ListView问题 防止对话框被关掉的代码


    本帖最后由 李少卿 于 2012-8-14 10:18 编辑

    AlertDialog.Builder.setAdapter方法可以给弹出的对话框设置个ListView

    我想点击这个ListView中的某几项给应用做一些设置

    但是点击某一项,事件响应后,对话框就关掉了

    就是说,点击ListView中的 某一项,效果跟点击下面的确定和取消是一样的

    对话框代码如下:

    代码片段,双击复制
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    final AlertDialog.Builder builder = new AlertDialog.Builder(
                                                            DetailedInterfaceActivity.this);
                                            builder.setTitle("请选择要下载的内容");
                                            final boolean[] bol = new boolean[course.length];
                                            final SelectAdapter sa = new SelectAdapter(course, bol);
                                            builder.setAdapter(sa, new DialogInterface.OnClickListener() {
                                                                    @Override
                                                                    public void onClick(DialogInterface dialog, int which) {
                                                                            bol[which] = !bol[which];
                                                                            sa.notifyDataSetChanged();
                                                                    }
                                                            })
                                                            .setPositiveButton("下载",
                                                                            new DialogInterface.OnClickListener() {
                                                                                    @SuppressWarnings("unchecked")
                                                                                    public void onClick(
    //此处是下载处理的代码
                                                                            })
                                                            .setNegativeButton("取消",
                                                                            new DialogInterface.OnClickListener() {
                                                                                    public void onClick(
                                                                                                    DialogInterface dialog,
                                                                                                    int which) {
                                                                                    }
                                                                            });
                                            builder.create().show();


    BaseAdapter的代码:

    代码片段,双击复制
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    class SelectAdapter extends BaseAdapter {
                     
                    String course[];
                    boolean value[];
                    CheckBox check[];
                     
                    SelectAdapter(String course[], boolean value[]){
                            this.course = course;
                            this.value = value;
                    }
     
                    @Override
                    public int getCount() {
                            check = new CheckBox[course.length];
                            return course.length;
                    }
     
                    @Override
                    public CheckBox getItem(int position) {
                            return check[position];
                    }
     
                    @Override
                    public long getItemId(int position) {
                            return position;
                    }
     
                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                             
                            LayoutInflater laInflater = (LayoutInflater) DetailedInterfaceActivity.this
                                    .getSystemService(android.app.Service.LAYOUT_INFLATER_SERVICE);
                            convertView = laInflater.inflate(R.layout.select_layout, null);
                             
                            TextView black = (TextView) convertView.findViewById(R.id.select_black_7531);
                            TextView blue = (TextView) convertView.findViewById(R.id.select_blue_7532);
                            CheckBox check = (CheckBox) convertView.findViewById(R.id.select_checkBox_7533);
                            this.check[position] = check;
                             
                            black.setText(course[position].subSequence(0, 5));
                            if(-1 != course[position].indexOf("###")){
                                    check.setVisibility(View.GONE);
                                    blue.setVisibility(View.VISIBLE);
                                    blue.setText("已下载");
                            } else {
                                    check.setChecked(value[position]);
                            }
                             
                            return convertView;
                    }
            }


    BaseAdapter调用的布局文件:

    代码片段,双击复制
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="#ffffff"
      android:orientation="horizontal" >
       
      <TextView
        android:id="@+id/select_black_7531"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="@color/black"
              android:layout_marginLeft="15dip"
              android:layout_marginTop="15dip"
              android:layout_marginBottom="5dip"
            android:layout_centerVertical="true"
              android:textSize="20sp"
              android:text="第 1 课" />
               
      <TextView
        android:id="@+id/select_blue_7532"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="@color/blue"
              android:textSize="16sp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dip"
            android:visibility="gone"
              android:text="已下载" />
               
      <CheckBox
        android:id="@+id/select_checkBox_7533"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dip" />
       
    </RelativeLayout>
     

    已经解决了

    防止对话框被关掉的代码

    代码片段,双击复制
    01
    02
    03
    04
    05
    06
    07
                                                                            try {
                                                                                    Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                                                                                    field.setAccessible(true);
                                                                                    field.set(dialog,false);
                                                                            } catch (Exception e) {
                                                                                    e.printStackTrace();
                                                                            }


    设置可以被关掉的代码:

    代码片段,双击复制
    01
    02
    03
    04
    05
    06
    07
                                                                                            try {
                                                                                                    Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                                                                                                    field.setAccessible(true);
                                                                                                    field.set(dialog,true);
                                                                                            } catch (Exception e) {
                                                                                                    e.printStackTrace();
                                                                                            }
  • 相关阅读:
    刷题0705 双指针思想归类
    基于Svelte3.x桌面端UI组件库Svelte UI
    Android Calendar 系统日历提醒、日程同步系统
    apk安装到系统文件夹
    Redis docker 主从模式与哨兵sentinel
    Nexus创建maven仓库导入maven依赖
    我们的力量的来源
    iOS项目嵌入Unity的View(Swift )
    面试题
    Alist+Raidriver+网盘
  • 原文地址:https://www.cnblogs.com/firecode/p/2704746.html
Copyright © 2020-2023  润新知