Android的Button点击事件,提示以及单选框和复选框的点击事件

本文详细介绍了Android中Button按钮的三种点击事件处理方式:内部类、匿名内部类和实现OnClickListener接口,以及如何进行日志输出、Toast提示。此外,还讲解了单选框RadioButton和复选框CheckBox的使用,包括它们的点击事件处理和常见应用场景。最后,通过一个综合示例展示了如何综合运用这些组件获取用户输入和验证。
摘要由CSDN通过智能技术生成

目录

1. Button按钮的点击事件

    1. 内部类

    2. 匿名内部类

    3. 实现onClickListener接口

2.提示

3.单选框 RadioButton

复选框 CheckBox

综合使用


1. Button按钮的点击事件

    1. 内部类

            单个按钮,代码量较多的时候

代码案例

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button but_login=findViewById(R.id.but_login);

    but_login.setOnClickListener(but_click);
}
private View.OnClickListener but_click = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.i("btn点击事件","but_login按钮被点击");
        Toast.makeText(MainActivity.this,"功能尚未完成,正在开发中",Toast.LENGTH_SHORT).show();
    }
};

    2. 匿名内部类

            单个按钮,并且代码量较少的时候

代码案例

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button but_login=findViewById(R.id.but_login);

    but_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.i("btu点击事件","but_login按钮被点击");
            Toast.makeText(MainActivity.this,"功能尚未升级,正在开发中",Toast.LENGTH_SHORT).show();
        }
    });
}

    3. 实现onClickListener接口

              多个按钮的时候

代码案例

public class MainActivity5 extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);

        Button but_1=findViewById(R.id.but_1);
        Button but_2=findViewById(R.id.but_2);
        Button but_3=findViewById(R.id.but_3);

        but_1.setOnClickListener(this);
        but_2.setOnClickListener(this);
        but_3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.but_1){
            Toast.makeText(MainActivity5.this,"按钮1已被点击",Toast.LENGTH_SHORT).show();
        }else if(v.getId()==R.id.but_2){
            Toast.makeText(MainActivity5.this,"按钮2已被点击",Toast.LENGTH_SHORT).show();
        }else if(v.getId()==R.id.but_3) {
            Toast.makeText(MainActivity5.this, "按钮3已被点击", Toast.LENGTH_SHORT).show();
        }
    }
}

2.提示

1.在控制台输出

在android中依然是可用的,但是不建议使用

System.out.println("hello world");

2.后台提示

        主要是给开发人员使用

 第一个参数 需要填写字符串 标签
 第二个参数 需要填写字符串 输出内容

 Log.i("登录操作","登录成功");  

3.前台提示

      主要是给用户使用  

Toast.makeText(MainActivity.this, "登录登录", Toast.LENGTH_SHORT).show();

3.单选框 RadioButton

1.单选框的独立存在: 主要用在同意协议等

2.单选框的点击事件(点击事件用在什么地方?)

单选框的点击事件是设置在RadioGroup上

onCheckedChangeListener() 

3..单选框一般情况下都是多个出现:比如性别,必须跟RadioGroup结合使用

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    orientation="vertical">
    
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="男"></RadioButton>
    
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="女"></RadioButton>
        
</RadioGroup>

代码案例

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);


    RadioGroup radioGroup = findViewById(R.id.rgp_gender);

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            RadioButton radioButton=findViewById(i);
            Toast.makeText(getApplicationContext(),"按钮组值发生了改变,你选了"+radioButton.getText(),Toast.LENGTH_SHORT).show();
        }
    });
}

复选框 CheckBox

1.复选框的独立使用:主要用于 同意协议

2.复选框的多个使用:多选

3.复选框的点击事件:

复选框的点击事件是设置在checkbox上

onCheckedChangeListener() 

代码案例

前台页面代码

<TextView
    android:id="@+id/textView3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="请选择下面手机是安卓系统的(多选)" />

<CheckBox
    android:id="@+id/xiaomi"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="小米" />

<CheckBox
    android:id="@+id/pingguo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="苹果" />

<CheckBox
    android:id="@+id/huawei"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="华为" />

<CheckBox
    android:id="@+id/oppo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="oppo" />

后台java代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main4);

    CheckBox xiaomi=findViewById(R.id.xiaomi);
    CheckBox huawei=findViewById(R.id.huawei);
    CheckBox pingguo=findViewById(R.id.pingguo);
    CheckBox oppo=findViewById(R.id.oppo);

    xiaomi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (b){
                String choice = compoundButton.getText().toString();
                Toast.makeText(MainActivity4.this,choice,Toast.LENGTH_SHORT).show();
            }
        }
    });
    huawei.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (b){
                String choice = compoundButton.getText().toString();
                Toast.makeText(MainActivity4.this,choice,Toast.LENGTH_SHORT).show();
            }
        }
    });
    pingguo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (b){
                String choice = compoundButton.getText().toString();
                Toast.makeText(MainActivity4.this,choice,Toast.LENGTH_SHORT).show();
            }
        }
    });
    oppo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (b){
                String choice = compoundButton.getText().toString();
                Toast.makeText(MainActivity4.this,choice,Toast.LENGTH_SHORT).show();
            }
        }
    });
}

综合使用

        如何取值 getText().toString();

代码案例

1.前台页面代码

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="账号:" />

    <EditText
        android:id="@+id/ad_zh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="number" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="密码:" />

    <EditText
        android:id="@+id/ad_mi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPassword" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textView6"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="密码重复:" />

    <EditText
        android:id="@+id/ad_fu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPassword" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_marginTop="7dp"
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="性别:" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <RadioButton
            android:id="@+id/re_man"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男" />

        <RadioButton
            android:layout_marginStart="20dp"
            android:id="@+id/re_women"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女" />
    </RadioGroup>



</LinearLayout>

<CheckBox
    android:layout_marginTop="150dp"
    android:id="@+id/cb_ad"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="你的年龄是否成年" />

<CheckBox
    android:id="@+id/cb_ac"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="请阅读并勾选协议" />

<Button
    android:id="@+id/but_logins"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="登录" />

后台Java代码

public class MainActivity6 extends AppCompatActivity {
    private CheckBox cb_ac;
    private  CheckBox cb_ab;
    private Button but_logins;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);

        cb_ac=findViewById(R.id.cb_ac);
        cb_ab=findViewById(R.id.cb_ad);
        but_logins=findViewById(R.id.but_logins);

        but_logins.setOnClickListener(but_click);
    }
    protected View.OnClickListener but_click = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           boolean flag=cb_ab.isChecked();
           boolean flai=cb_ac.isChecked();
           if (flag&& flai){
               EditText ad_zh=findViewById(R.id.ad_zh);
               EditText ad_mi=findViewById(R.id.ad_mi);
               EditText ad_fu=findViewById(R.id.ad_fu);

               String repwd=ad_fu.getText().toString();
               String pwd=ad_mi.getText().toString();
               String phonenum=ad_zh.getText().toString();

               RadioButton re_man=findViewById(R.id.re_man);
               RadioButton re_woman=findViewById(R.id.re_women);
               String sex="";
               if (re_man.isChecked()){
                   sex=re_man.getText().toString();
               }else {
                   sex=re_woman.getText().toString();
               }
               Toast.makeText(MainActivity6.this, "电话号码是:"+phonenum+",密码是:"+pwd+"," +
                       "第二次输入的密码是:"+repwd+",性别是:"+sex, Toast.LENGTH_LONG).show();
           }else {
               Toast.makeText(MainActivity6.this,"请勾选协议",Toast.LENGTH_SHORT).show();
           }
        }
    };
}

实现图

  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将单选框复选框的数据添加到SQLite数据库中,您需要执行以下步骤: 1. 创建一个SQLite数据库并创建一个表来存储数据。 2. 在您的Android应用程序中,使用单选框复选框来收集用户数据。 3. 将单选框复选框的值转换为字符串,并将其插入到SQLite数据库表中。 以下是一个简单的示例: 创建一个SQLite数据库并创建一个表来存储数据: ```java public class DatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "mydatabase.db"; private static final int DATABASE_VERSION = 1; private static final String TABLE_NAME = "mytable"; private static final String COLUMN_ID = "id"; private static final String COLUMN_CHECKBOX = "checkbox"; private static final String COLUMN_RADIOBUTTON = "radiobutton"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String createTable = "CREATE TABLE " + TABLE_NAME + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_CHECKBOX + " TEXT, " + COLUMN_RADIOBUTTON + " TEXT)"; db.execSQL(createTable); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } } ``` 在您的Android应用程序中,使用单选框复选框来收集用户数据: ```java public class MainActivity extends AppCompatActivity { private CheckBox checkBox; private RadioButton radioButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkBox = findViewById(R.id.checkbox); radioButton = findViewById(R.id.radiobutton); Button saveButton = findViewById(R.id.save_button); saveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { saveDataToDatabase(); } }); } private void saveDataToDatabase() { String checkboxValue = checkBox.isChecked() ? "true" : "false"; String radioButtonValue = radioButton.isChecked() ? "selected" : "not selected"; DatabaseHelper dbHelper = new DatabaseHelper(this); SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(DatabaseHelper.COLUMN_CHECKBOX, checkboxValue); values.put(DatabaseHelper.COLUMN_RADIOBUTTON, radioButtonValue); long newRowId = db.insert(DatabaseHelper.TABLE_NAME, null, values); if (newRowId == -1) { Toast.makeText(this, "Error saving data to database!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Data saved to database!", Toast.LENGTH_SHORT).show(); } } } ``` 将单选框复选框的值转换为字符串,并将其插入到SQLite数据库表中: 在 `saveDataToDatabase()` 方法中,我们将单选框复选框的值转换为字符串,并将这些值插入到SQLite数据库表中。我们使用 `ContentValues` 对象来存储列名和值的映射,然后调用 `insert()` 方法将该行插入到数据库表中。 请注意,我们使用 `isChecked()` 方法来检查复选框是否选中,以及使用 `isChecked()` 方法来检查单选框是否被选中。如果复选框单选框选中,我们将字符串值设为 "true" 或 "selected",否则设为 "false" 或 "not selected"。 希望这可以帮助您将单选框复选框的数据添加到SQLite数据库中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值