开发者

Create and validate check box programmatically using Java in Android

开发者 https://www.devze.com 2023-04-08 01:16 出处:网络
I need to create a number of check boxes programmatically using Java, check if one of them is checked and when I click Next Button the control will move to next page. If none of them is checked a toas

I need to create a number of check boxes programmatically using Java, check if one of them is checked and when I click Next Button the control will move to next page. If none of them is checked a toast message has to be displayed. Please suggest as I am new to Android.

Here is my code:

public class TestValidCheckboxActivity extends Activity implements OnCheckedChangeListener{

private RelativeLayout layoutMiddle = null;
private TableLayout layout1;
private CheckBox chk;
private  String[] resarr = {"silu", "pinky", "meera"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    layoutMiddle = (RelativeLayout)findViewById(R.id.layoutMiddleUp);
    layout1 = (TableLayout)findViewById(R.id.tableId1);
    final Button btnNext = (Button)findViewById(R.id.btnNext);
    int i = 0;
        for(String res: resarr){
            TableRow tr=new TableRow(this);
                chk=new CheckBox(this);
                chk.setId(i);
                chk.setText(res);
                chk.setTextColor(Color.BLACK);
                tr.addView(chk);
                i++;
            RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
            lay.addRule(RelativeLayout.BELOW, layoutMiddle.getId());
            layout1.addView(tr, lay);
        }
        chk.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
                if (arg1)
                {
                    btnNext.setOnClickListener(new View.OnClickListener() {
                        public void onClick(final View view) {
                            Log.e("1111111111","111111111");
                                if(chk.isChecked()){
                                    Intent intent = new Intent(view.getContext(),NextPage.class);
                                    startActivityForResult(intent, 0);
                                }else{
                                    Toast msg = Toast.makeText(view.getContext(), "please choose at least one option", Toast.LENGTH_LONG);
                                    msg.show();
                                }

                        }
                    });
                }else{
                    Toast msg = Toast.makeText(TestValidCheckboxActivity.this, "please choose at least one option", Toast.LENGTH_LONG);
                }
            }

        });

    }
    @Override
    public void onCheckedChanged(CompoundButt开发者_StackOverflow中文版on arg0, boolean arg1) {
    // TODO Auto-generated method stub

    }
}


this can be easily done take a boolean flag and set true it on checkbox.isChecked();(inbuilt) then check if flag is set then navigate to next else TOAST


Can you show us some of your layout code, meaning, are the checkboxes defined in an xml file and do they have IDs?

If they do, you can access each of them with findViewByID and check their status one by one.

0

精彩评论

暂无评论...
验证码 换一张
取 消