zoukankan      html  css  js  c++  java
  • 弹出对话框中的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();
                                                                                            }
  • 相关阅读:
    各地电信运营商插广告赚钱,北京联通也不甘落后
    也谈Server Limit DOS的解决方案
    Still Believe
    无奈小虫何
    好朋有也有类别
    无为而治
    青鸟随想
    落寞时分
    网站开发学习路线和资料
    C++实例 添加快捷键表
  • 原文地址:https://www.cnblogs.com/firecode/p/2704746.html
Copyright © 2011-2022 走看看