安卓开发-Android Studio-前端-设置单选框和复选框

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/monster_ayb/article/details/82177140

                安卓开发-Android Studio-前端-设置单选框和复选框

1.新建一个radioButtonActivity

老规矩 在MainActivity中设置点击事件 与RadioButtonActivity相关联

package com.example.ayb.helloworld;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private Button mBtnTextView;
    private Button mBtnButton;
    private Button mBtnEditText;
    private Button mBtnRadioButton;
    private Button mBtnCheckBox;
    private Button mBtnImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBtnTextView = findViewById(R.id.btn_textview);
        mBtnButton = findViewById(R.id.btn_button);
        mBtnEditText = findViewById(R.id.btn_edittext);
        mBtnRadioButton = findViewById(R.id.btn_radiobutton);
        mBtnCheckBox = findViewById(R.id.btn_checkbox);
        mBtnImageView = findViewById(R.id.btn_imageview);
        setOnClickListener();
    }
    private void setOnClickListener(){
        OnClick onClick = new OnClick();
        mBtnTextView.setOnClickListener(onClick);
        mBtnRadioButton.setOnClickListener(onClick);
        mBtnEditText.setOnClickListener(onClick);
        mBtnButton.setOnClickListener(onClick);
        mBtnCheckBox.setOnClickListener(onClick);
        mBtnImageView.setOnClickListener(onClick);
    }

    private class  OnClick  implements View.OnClickListener{

        @Override
        public void onClick(View v){
            Intent intent = null;
            switch (v.getId()){
                case R.id.btn_textview:
                    intent = new Intent(MainActivity.this,TextViewActivity.class);
                    break;
                case R.id.btn_button:
                    intent = new Intent(MainActivity.this,ButtonActivity.class);
                    break;
                case R.id.btn_edittext:
                    intent = new Intent(MainActivity.this, EditTextActivity.class);
                    break;
                case R.id.btn_radiobutton:
                    intent = new Intent(MainActivity.this, RadioButtonActivity.class);
                    break;
                case R.id.btn_checkbox:
                    intent = new Intent(MainActivity.this, CheckBoxAcitivity.class);
                    break;
                case R.id.btn_imageview:
                    intent = new Intent(MainActivity.this, ImageViewActivity.class);
                    break;
            }
            startActivity(intent);
        }
    }
}

RadioButtonActivity中的布局代码如下

<?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="match_parent"
    android:orientation="vertical"
    >

    <RadioGroup
        android:id="@+id/rg_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
         <RadioButton
            android:id="@+id/rb_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:textSize="16sp"
            android:textColor="#c50f57"
            />
        <RadioButton
            android:id="@+id/rb_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:layout_below="@id/rb_1"
            android:textSize="16sp"
            android:textColor="#c50f57"
            />
        </RadioGroup>
    <RadioGroup
        android:id="@+id/rg_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/rg_1"
        android:layout_marginTop="40dp"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/rb_3"
            android:layout_width="40dp"
            android:gravity="center"
            android:layout_height="wrap_content"
            android:text="男"
            android:button="@null"
            android:textSize="16sp"
            android:layout_marginLeft="30dp"
            android:textColor="#000000"
            android:background="@drawable/selector_orange_radiobutton"
            />
        <RadioButton
            android:id="@+id/rb_4"
            android:layout_width="40dp"
            android:gravity="center"
            android:layout_height="wrap_content"
            android:text="女"
            android:button="@null"
            android:layout_below="@id/rb_1"
            android:layout_marginLeft="20dp"
            android:background="@drawable/selector_orange_radiobutton"
            android:textSize="16sp"
            android:textColor="#000000"
            />
    </RadioGroup>
</RelativeLayout>

    <RadioGroup //设置一组单选框
        android:id="@+id/rg_1"
        android:layout_width="wrap_content"   // 根据内容填充框架
        android:layout_height="wrap_content"
        android:orientation="vertical"> // 设置垂直分布
         <RadioButton
            android:id="@+id/rb_1"  // 设置id 方便设置监听事件
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"  // 设置按钮显示内容
            android:textSize="16sp"// 设置内容文字大小
            android:textColor="#c50f57" // 设置内容文字颜色
            />
        <RadioButton
            android:id="@+id/rb_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:layout_below="@id/rb_1" // 由于是相对布局 设置位置
            android:textSize="16sp"
            android:textColor="#c50f57"
            />
        </RadioGroup>

设置监听事件

package com.example.ayb.helloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class RadioButtonActivity extends AppCompatActivity {

    private RadioGroup nRg1;
    private RadioGroup nRg2; //声明单选框组

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

        nRg1 = findViewById(R.id.rg_1);
        nRg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton radioButton = radioGroup.findViewById(i);
                Toast.makeText(RadioButtonActivity.this,radioButton.getText(),Toast.LENGTH_SHORT).show();
             }
        });

        nRg2 = findViewById(R.id.rg_2);
        nRg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton radioButton = radioGroup.findViewById(i);
                Toast.makeText(RadioButtonActivity.this,radioButton.getText(),Toast.LENGTH_SHORT).show();
            }
        });
    }
}

监听事件显示结果

设置复选框组

在CheckBoxActivity中代码如下

<?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="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你会那些移动开发?"
        android:textSize="20sp"
        android:textColor="#000000"
        android:layout_marginBottom="10sp"
        />


    <CheckBox
        android:id="@+id/cb_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android"
        android:textSize="20sp"
        android:layout_below="@id/tv_title"
        />
    <CheckBox
        android:id="@+id/cb_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="IOS"
        android:textSize="20sp"
        android:layout_below="@id/cb_1"
        />
    <CheckBox
        android:id="@+id/cb_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="H5"
        android:textSize="20sp"
        android:layout_below="@id/cb_2"
        />
    <CheckBox
        android:id="@+id/cb_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="其他"
        android:textSize="20sp"
        android:layout_below="@id/cb_3"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@id/cb_4"
        android:layout_marginTop="20dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="你的兴趣"
            android:textColor="#000000"
            android:textSize="20sp"
            />
        <CheckBox
            android:id="@+id/cb_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="编程"
            android:layout_marginTop="10dp"
            />
        <CheckBox
            android:id="@+id/cb_6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="做饭"
            android:textSize="20sp"
            />

    </LinearLayout>


</RelativeLayout>

显示结果如下

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

展开阅读全文

没有更多推荐了,返回首页