Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何感知动态android复选框点击和保存状态_Android_Checkbox - Fatal编程技术网

如何感知动态android复选框点击和保存状态

如何感知动态android复选框点击和保存状态,android,checkbox,Android,Checkbox,我已经回答了各种与复选框相关的问题,但仍在努力寻找适合我问题的答案。 我有一个复选框的正常要求,可以让用户选中和取消选中。“层”由 用户将在背景地图中可见,未选中的ONCE将不可见 我的问题是,我无法正确地维护当 对话框每次都会打开。当前,假设我选中图层1和图层5并将它们设置为可见 然后我取消选中刚才的第1层以后,为了消失只有1;1和5都消失了。 我觉得这可能是因为我正在使用全局变量来设置布尔值。如果有人能帮助我,那就太好了。谢谢 布局层_checkbox.xml: <?xml vers

我已经回答了各种与复选框相关的问题,但仍在努力寻找适合我问题的答案。 我有一个复选框的正常要求,可以让用户选中和取消选中。“层”由 用户将在背景地图中可见,未选中的ONCE将不可见

我的问题是,我无法正确地维护当 对话框每次都会打开。当前,假设我选中图层1和图层5并将它们设置为可见 然后我取消选中刚才的第1层以后,为了消失只有1;1和5都消失了。 我觉得这可能是因为我正在使用全局变量来设置布尔值。如果有人能帮助我,那就太好了。谢谢

布局层_checkbox.xml:

<?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" > 

            <TextView
              android:id="@+id/label"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_toRightOf="@+id/check"
              android:text="@+id/label"
              android:textSize="20sp"
              android:padding="8dp"
              android:textColor="#0d00ff" >
           </TextView>

          <CheckBox
             android:id="@+id/check"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignParentLeft="true"
             android:layout_marginLeft="4px"
             android:layout_marginRight="10px" 
             >
          </CheckBox>    

</RelativeLayout> 

我的做法如下: 1.initLayerManagementDialog()方法:

//这两个是全局变量。
public CharSequence[]charSequenceItems;
公共布尔校验1[];
//单击“层”选项时调用此方法。
public void initLayerManagementDialog()
{
AlertDialog.Builder=新建AlertDialog.Builder(此);
建造商名称(“层”);
警报对话框;
charSequenceItems=map.getLayerList().toArray(新的CharSequence[map.getLayerList().size()]);
最终布尔值[]选中=新布尔值[charSequenceItems.length];
checked1=新布尔值[charSequenceItems.length];
LayerDatapter ladapter=新的LayerDatapter(myInstance.MapApplicationContext,map.getLayerList());
setAdapter(ladapter,新的DialogInterface.OnClickListener(){
//这是一个空的onClick方法。正在寻找一种更干净的方法来避免这种情况。
@凌驾
public void onClick(DialogInterface dialog,int which){
System.out.println(“复选框按钮点击>>”;
}       
});                 
setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int-id){
对于(inti=0;i
  • 自定义适配器:

        public class LayerAdapter extends ArrayAdapter {
      private Context context;
      private List<String> SymbolList;
      boolean isChecked = false;
    
      public LayerAdapter(Context pContext, List<String> pSymbolList)
      {
        super(pContext, R.layout.layer_checkbox, pSymbolList); 
        this.context = pContext;
        this.SymbolList = pSymbolList;
      }
    
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.layer_checkbox, parent, false);
        final CheckBox cb  = (CheckBox) rowView.findViewById(R.id.check);
        cb.setChecked(map.getLayerByName(map.getLayerList().get(position)).getVisibility());
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        textView.setText(SymbolList.get(position));  
        final int posit = position;                    
        cb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {   
                if (((CheckBox) v).isChecked()) {       
                    checked1[posit] = true;      
    
                } else {    
    
                    checked1[posit] = false;
                }   
    
            }
        });     
    
        return rowView;
        }
        }
    
    公共类LayerDatapter扩展了ArrayAdapter{
    私人语境;
    私人列表符号列表;
    布尔值isChecked=false;
    公共层标识符(上下文pContext,列表pSymbolList)
    {
    super(pContext,R.layout.layer_复选框,pSymbolList);
    this.context=pContext;
    this.SymbolList=心理主义者;
    }
    @凌驾
    公共视图getView(int位置、视图转换视图、视图组父视图){
    LayoutFlater充气器=(LayoutFlater)上下文
    .getSystemService(上下文布局\充气机\服务);
    视图行视图=充气机。充气(R.layout.layer\u复选框,父项,false);
    最终复选框cb=(复选框)rowView.findViewById(R.id.check);
    cb.setChecked(map.getLayerByName(map.getLayerList().get(position)).getVisibility());
    TextView TextView=(TextView)rowView.findViewById(R.id.label);
    setText(SymbolList.get(position));
    最终积分位置=位置;
    cb.setOnClickListener(新的OnClickListener(){
    @凌驾
    公共void onClick(视图v){
    如果(((复选框)v).isChecked()){
    checked1[posit]=真;
    }否则{
    checked1[posit]=假;
    }   
    }
    });     
    返回行视图;
    }
    }
    

  • 布局xml:

    您不需要使用适配器,因为对话框生成器可以自动为您创建复选框列表。您也不需要为数组使用成员变量

    public void initLayerManagementDialog()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Layers"); 
        final CharSequence[] charSequenceItems = map.getLayerList().toArray(new CharSequence[map.getLayerList().size()]);       
        final boolean [] checkedItems = new boolean[charSequenceItems.length];
    
        for (int i=0; i<checkedItems.length; i++){
            checkedItems[i] = map.getLayerByName(map.getLayerList().get(i).getVisibility());
        }
    
        builder.setMultiChoiceItems(charSequenceItems, checkedItems, new OnMultiChoiceClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int indexSelected,
                    boolean isChecked) {
                checkedItems[indexSelected] = isChecked;
            }
        }); 
    
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {                 
                for(int i = 0; i < map.getLayerList().size();i++){                     
                map.setLayerVisibility(  map.getLayerByName(map.getLayerList().get(i)).getName(), checkedItems[i]);
                }                                       
                mapView.invalidate();    
            }
        });     
    
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {                            
            }
        });
    
        AlertDialog dialog=builder.create();
        dialog.show();
        dialog.setCanceledOnTouchOutside(true);
    }
    
    public void initlayer管理对话框()
    {
    AlertDialog.Builder=新建AlertDialog.Builder(此);
    建造商名称(“层”);
    final CharSequence[]charSequenceItems=map.getLayerList().toArray(新的CharSequence[map.getLayerList().size()]);
    最终布尔值[]checkedItems=新布尔值[charSequenceItems.length];
    
    对于(int i=0;i当你旋转手机时,你何时会松开状态?我总是松开状态。感谢Tenfour04的建议。事实上我已经尝试过这种方法。但问题是,由于你的方法不需要布局,所以在将复选框安排到所需位置时,我没有任何灵活性。(即文本左侧)使用您的方法是否可以拥有我在图表中提到的类似布局。这种方法不允许您重新安排布局。在我个人看来,如果您的功能不需要标准UI布局,最好避免改变它,因为这会让用户迷失方向。但是如果您想使用您的方法,我认为ink您的问题是,在您选中和取消选中复选框时,字符串列表没有更新,因此它们正在丢失状态。您需要将复选框状态单独链接到布尔数组。我将为
    布尔[]添加一个参数
    到适配器的构造函数,这样您就可以传入
    选中的
    数组,并将对该数组的引用作为成员变量存储在那里,而不是存储在外部类中。在传入数组之前,使用映射中的当前值初始化该数组,如上所述。然后在
    getView()中
    ,改为使用布尔数组初始化复选框
    public void initLayerManagementDialog()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Layers"); 
        final CharSequence[] charSequenceItems = map.getLayerList().toArray(new CharSequence[map.getLayerList().size()]);       
        final boolean [] checkedItems = new boolean[charSequenceItems.length];
    
        for (int i=0; i<checkedItems.length; i++){
            checkedItems[i] = map.getLayerByName(map.getLayerList().get(i).getVisibility());
        }
    
        builder.setMultiChoiceItems(charSequenceItems, checkedItems, new OnMultiChoiceClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int indexSelected,
                    boolean isChecked) {
                checkedItems[indexSelected] = isChecked;
            }
        }); 
    
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {                 
                for(int i = 0; i < map.getLayerList().size();i++){                     
                map.setLayerVisibility(  map.getLayerByName(map.getLayerList().get(i)).getName(), checkedItems[i]);
                }                                       
                mapView.invalidate();    
            }
        });     
    
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {                            
            }
        });
    
        AlertDialog dialog=builder.create();
        dialog.show();
        dialog.setCanceledOnTouchOutside(true);
    }