简体   繁体   English

检测哪个复选框被触摸? 数组 Java Android Studio

[英]Detect which checkbox is touched? Array Java Android Studio

I have a CheckBox ArrayList set up, but how do I know which one is touched?我有一个CheckBox ArrayList设置,但我怎么知道哪个被触摸了? More specifically I simply would like to get the integer value of "i" in the ArrayList of which checkbox has been touched.更具体地说,我只是想在已触摸复选框的ArrayList中获取“i”的整数值。 Any ideas?有任何想法吗?

@Override
    public void sendData(String userText, String userNotes) {

        //we can create the saves here as well....

        LinearLayout ll = (LinearLayout)findViewById(R.id.myLayout);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        CheckBox tv = new CheckBox(getApplicationContext());
        tv.setTextSize(20);
        tv.setText(userText + "  \n" + date);
        tv.setLayoutParams(lp);
        checkBoxArrayList.add(tv);

        //Handles if checkbox is pressed down, and set id for each check box
        for(int i = 0; i < checkBoxArrayList.size(); i ++){

            checkBoxArrayList.get(i).setId(i);

            checkBoxArrayList.get(i).setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {

                    switch(motionEvent.getAction()) {

                        case MotionEvent.ACTION_DOWN:
                            //here we will simply open the files unless held down

                            //when held down
                            view.postDelayed(runnable, 1250);
                            break;

                    }
                    return false;
                }
            });
            }

            //add checkbox passed in to the layout
            ll.addView(tv);
        }

Thanks to @rex I simply had a global private integer checkboxPopupCheck.感谢@rex,我只有一个全局私有整数 checkboxPopupCheck。 I added it inside of the case MotionEvent.ACTION_DOWN.我将它添加到案例 MotionEvent.ACTION_DOWN 中。 I can now use this integer for what I need.我现在可以使用这个整数来满足我的需要。

checkboxPopupCheck = view.getId();

Your onTouch Method has two parameters, the first is the view that was touched.你的 onTouch 方法有两个参数,第一个是被触摸的视图。

So doing a simple view.getId() would return you the same id that you set with所以做一个简单的view.getId()会返回你设置的相同 id

checkBoxArrayList.get(i).setId(i);

and that's the integer value of the ArrayList这是 ArrayList 的整数值

Try this code inside your adapter where clicking the check box a new array list you have to create and add the position or the value of the holder in to the array list thus you can know the what are the items are being checked.在您的适配器中尝试此代码,其中单击复选框,您必须创建一个新的数组列表,并将持有者的位置或值添加到数组列表中,这样您就可以知道正在检查哪些项目。

 holderItem.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ // add in to a arraylist }else{ // remove it from arraylist } } });

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

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