• Android checkbox和radiobutton 以及Toast和AlertDialog的使用


    package com.example.radiobutton_01;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.Toast;
    
    public class MyActivity extends Activity {
        private RadioGroup rg;
        private Button btn;
        /**
         * Called when the activity is first created.
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            rg = (RadioGroup)findViewById(R.id.rg);
            btn = (Button)findViewById(R.id.btn);
    
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int len = rg.getChildCount();
                    String msg = "";
                    for(int i=0;i<len;i++) {
                        RadioButton rb = (RadioButton)rg.getChildAt(i);
                        if(rb.isChecked()) {
                            msg = rb.getText().toString();
                            break;
                        }
                    }
    
                    Toast.makeText(MyActivity.this,msg,Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    

    package com.example.checkbox_01;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.LinearLayout;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MyActivity extends Activity implements View.OnClickListener{
        private List<CheckBox> checkBoxes = new ArrayList<CheckBox>();
        /**
         * Called when the activity is first created.
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.main);
    
            String[] checkBoxText = new String[]{
                "are you student?

    ","are you love android?","are you dev?" }; LinearLayout linearLayout = (LinearLayout)getLayoutInflater().inflate(R.layout.main,null); for(int i=0;i<checkBoxText.length;i++) { CheckBox checkBox = (CheckBox)getLayoutInflater().inflate(R.layout.checkbox,null); checkBox.setText(checkBoxText[i]); checkBoxes.add(checkBox); linearLayout.addView(checkBox,i); } setContentView(linearLayout); Button btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(this); } @Override public void onClick(View view) { String s = ""; for(CheckBox cb : checkBoxes) { if(cb.isChecked()) { s+=cb.getText() + " "; } } if("".equals(s)) { s = "你没有选择选项"; } new AlertDialog.Builder(this).setMessage(s).setPositiveButton("关闭",null).show(); } }


  • 相关阅读:
    win7下的vxworks总结
    ubuntu 无法获得锁 /var/lib/dpkg/lock
    项目中用到了的一些批处理文件
    win7下安装 WINDRIVER.TORNADO.V2.2.FOR.ARM
    使用opencv统计视频库的总时长
    January 05th, 2018 Week 01st Friday
    January 04th, 2018 Week 01st Thursday
    January 03rd, 2018 Week 01st Wednesday
    January 02nd, 2018 Week 01st Tuesday
    January 01st, 2018 Week 01st Monday
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5077623.html
Copyright © 2020-2023  润新知