经常使用控件——单选按钮和复选框(设置基本信息)

涉及知识点
一、线性布局(LinearLayout)
二、标签(TextView)
三、按钮(Button)
四、编辑框(EditText)
五、单选按钮组(RadioGroup)
六、单选按钮(RadioButton)
七、复选框(CheckBox)android

根据相关知识点作的脑图
在这里插入图片描述
咱们如今作的是第一个案例:设置基本信息
1.思考要怎样作布局
2.准备背景图片
3.xml代码web

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/background"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="30dp" >

    <TextView
        android:id="@+id/tv_set_information"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="30dp"
        android:text="@string/set_information"
        android:textColor="#0000ff"
        android:textSize="30sp" />

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

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/name"
            android:textColor="#000000"
            android:textSize="16sp" />

        <EditText
            android:id="@+id/edt_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/input_name"
            android:singleLine="true" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/tv_gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/gender"
            android:textColor="#000000"
            android:textSize="16sp" />

        <RadioGroup
            android:id="@+id/rg_gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/rb_male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="@string/male" />

            <RadioButton
                android:id="@+id/rb_female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:text="@string/female" />
        </RadioGroup>
    </LinearLayout>

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

        <TextView
            android:id="@+id/tv_hobby"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hobby"
            android:textColor="#000000"
            android:textSize="16sp" />

        <CheckBox
            android:id="@+id/cb_music"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/music" />

        <CheckBox
            android:id="@+id/cb_read"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/read" />

        <CheckBox
            android:id="@+id/cb_food"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/food" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp" >

        <Button
            android:id="@+id/btn_ok"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="doOK"
            android:text="@string/ok" />

        <Button
            android:id="@+id/btn_clear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="doClear"
            android:text="@string/clear" />

        <Button
            android:id="@+id/btn_exit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="doExit"
            android:text="@string/exit" />
    </LinearLayout>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:textSize="15sp" />

</LinearLayout>

4.string代码app

<resources>
    <string name="app_name">设置基本信息</string>
    <string name="male">男</string>
    <string name="female">女</string>
    <string name="single">单身</string>
    <string name="married">已婚</string>
    <string name="music">音乐</string>
    <string name="read">阅读</string>
    <string name="food">美食</string>
    <string name="ok">肯定</string>
    <string name="clear">清除</string>
    <string name="exit">退出</string>
    <string name="action_settings">设置</string>
    <string name="set_information">设置基本信息</string>
    <string name="gender">性别:</string>
    <string name="marriage">婚姻:</string>
    <string name="hobby">爱好:</string>
    <string name="name">姓名:</string>
    <string name="input_name">请输入姓名</string>
</resources>

5.Java代码ide

package net.hw.setbasicinfornation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends Activity {
    private EditText edtName;
    private RadioGroup rgGender;
    private RadioButton rbMale;
    private RadioButton rbFemale;
    private CheckBox cbMusic;
    private CheckBox cbFood;
    private CheckBox cbRead;
    private TextView tvResult;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //利用布局资源文件设置用户界面
        setContentView(R.layout.activity_main);

        //经过资源标识得到控件实例
        edtName = (EditText) findViewById(R.id.edt_name);
        rgGender = (RadioGroup) findViewById(R.id.rg_gender);
        rbMale = (RadioButton) findViewById(R.id.rb_male);
        rbFemale = (RadioButton) findViewById(R.id.rb_female);
        cbRead = (CheckBox) findViewById(R.id.cb_read);
        cbMusic = (CheckBox) findViewById(R.id.cb_music);
        cbFood = (CheckBox) findViewById(R.id.cb_food);
        tvResult = (TextView) findViewById(R.id.tv_result);

    }

    /**
     * 肯定按钮单击事件处理方法
     * @param view
     */
    public void doOK(View view){
        //获取姓名
        String strName = edtName.getText().toString();
        //获取性别
        String strGender ="";
        switch (rgGender.getCheckedRadioButtonId()){
            case R.id.rb_male:
                strGender = rbMale.getText().toString();
                break;
            case R.id.rb_female:
                strGender = rbFemale.getText().toString();
                break;
        }
        //获取爱好
        StringBuilder builder = new StringBuilder();
        if(cbMusic.isChecked()){
            builder.append(cbMusic.getText().toString()+"、");
        }
        if(cbFood.isChecked()){
            builder.append(cbFood.getText().toString()+"、");
        }
        if(cbRead.isChecked()){
            builder.append(cbRead.getText().toString());
        }
        String strHobbies = builder.toString();
        strHobbies = strHobbies.substring(0,strHobbies.length()-1);

        //组合设置的信息
        String strInFormation = "姓名:"+strName + "\n性别:" + strGender + "\n爱好:"+strHobbies;
        //在标签中显示设置的信息
        tvResult.setText(strInFormation);
    }

    /**
     * 清除按钮单击事件处理方法
     * @param view
     */
    public void doClear(View view){
        edtName.setText("");
        rbMale.setChecked(true);
        cbMusic.setChecked(false);
        cbRead.setChecked(false);
        cbFood.setChecked(false);
        tvResult.setText("");
        edtName.requestFocus();

    }

    /**
     * 退出按钮单击事件处理方法
     * @param view
     */
    public void doExit(View view){
         finish();
    }
}

6.打开程序后的效果图,字体颜色我是随便调的,不喜欢的能够按照本身喜欢的颜色来调
在这里插入图片描述
7.输入信息
在这里插入图片描述
8.单击肯定按钮的效果图
在这里插入图片描述
9.清除和退出的效果也是有的,只是我没有截图,大家能够本身试一下,期待大家的做品哦!svg