android學習之旅(9)---選擇和開關按鈕

單選框和複選框

單選框(RadioButton)和複選框(CheckBox)都是繼承時了Button類,因此它們都支持Button支持的各種屬性和方法。RadioBurron和CheckBox的區別在於:一組RadioButton每次只能選擇一個,所以RadioButton需要和RadioGroup一起使用。

示例程序

XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="15dp">
    <!--單選框必須放在單選組框中-->
    <RadioGroup
        android:id="@+id/sexRadioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="15dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:text="性別:"
            android:textColor="@color/colorBlue"
            android:textSize="24sp" />

        <RadioButton
            android:id="@+id/manRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:text="男"
            android:textSize="24sp" />

        <RadioButton
            android:id="@+id/womenRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:textSize="24sp" />
    </RadioGroup>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:paddingLeft="25dp"
        android:text="你喜歡喫的食物:"
        android:textColor="@color/colorOrange"
        android:textSize="22sp" />

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="15dp">

            <CheckBox
                android:id="@+id/checkbox1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="西紅柿"
                android:textSize="24sp" />

            <CheckBox
                android:id="@+id/checkbox2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="辣椒"
                android:textSize="24sp" />

            <CheckBox
                android:id="@+id/checkbox3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="白菜"
                android:textSize="24sp" />

            <CheckBox
                android:id="@+id/checkbox4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="土豆"
                android:textSize="24sp" />

            <CheckBox
                android:id="@+id/checkbox5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="雞蛋"
                android:textSize="24sp" />
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>

.java文件

package com.kong.viewtext;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/*
(1)創建一個繼承了AppCompatActivity的類(Activity的本質就是類)
(2)在活動清單中聲明該活動,不然會找不到。
(3)重寫onCreate方法
(4)創建對應的XML文件(如果需要的話)
(5)設置該界面的UI(setContentView)
 */

public class RadioButtonTestActivity extends AppCompatActivity {
    private RadioGroup m_RadioGroup;
    private TextView m_TextView;
    private CheckBox check1,check2,check3,check4,check5;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radiobutton_text);
        m_RadioGroup=findViewById(R.id.sexRadioGroup);
        m_RadioGroup.setOnCheckedChangeListener(new MyOnCheckedChange());
        m_TextView=findViewById(R.id.resultsTextView);

        CheckBoxCheckedChangeListener changeListener=new CheckBoxCheckedChangeListener();
        check1=findViewById(R.id.checkbox1);
        check1.setOnCheckedChangeListener(changeListener);
        check2=findViewById(R.id.checkbox2);
        check2.setOnCheckedChangeListener(changeListener);
        check3=findViewById(R.id.checkbox3);
        check3.setOnCheckedChangeListener(changeListener);
        check4=findViewById(R.id.checkbox4);
        check4.setOnCheckedChangeListener(changeListener);
        check5=findViewById(R.id.checkbox5);
        check5.setOnCheckedChangeListener(changeListener);

    }
    private class CheckBoxCheckedChangeListener implements CompoundButton.OnCheckedChangeListener
    {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked)
            {
                Toast.makeText(RadioButtonTestActivity.this,buttonView.getText(),Toast.LENGTH_SHORT).show();
            }
        }
    }
    //重寫RadioGroup的onChangedListener
    private class MyOnCheckedChange implements RadioGroup.OnCheckedChangeListener
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            String str=((RadioButton)(group.findViewById(checkedId))).getText().toString();
            Toast.makeText(RadioButtonTestActivity.this,str,Toast.LENGTH_SHORT).show();
        }
    }
}

運行結果

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-Ug6qRf6B-1575802526964)(./RadioButton運行結果.png)]

狀態開關按鈕和開關

轉態開關按鈕(ToggleButton)和開關(switch)也是由Button派生出來的,因此它們的本質也是按鈕,Button支持的各種屬性,方法也是用於ToggleButton和Switc。從功能上來看,ToggleButton、switch與CheckBox複選框非常的相似,它們都可以提供了兩種狀態。不過,ToggleButton、switch與checkBox的區別主要體現在功能上,ToggleButton、switch通常用於切換程序中的某種狀態。

示例程序

XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:padding="15dp">

    <ImageView
        android:id="@+id/imageLight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/on_light"
        android:layout_marginBottom="20dp"/>

    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textOn="開燈"
        android:textOff="關燈"
        android:textSize="24sp"
        android:background="@color/colorIndigo"/>

    <Switch
        android:id="@+id/switchDark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="黑夜模式"
        android:textColor="@color/colorOrange"
        android:layout_gravity="left"
        android:textSize="24sp"/>

</LinearLayout>

.java文件

package com.kong.viewtext;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.ToggleButton;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class ToggleButtonTestActivity extends AppCompatActivity {
    private ImageView m_ImageLight;
    private ToggleButton m_ToggleButton;
    private LinearLayout m_Linear;
    private Switch m_Switch;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_togglebutton_test);
        m_ImageLight = findViewById(R.id.imageLight);
        m_ToggleButton = findViewById(R.id.toggleButton);
        m_Linear = findViewById(R.id.linear);
        m_Switch = findViewById(R.id.switchDark);

        //m_Switch.setOnCheckedChangeListener(new ToggleButtonOnCheckChange());

        m_ToggleButton.setOnCheckedChangeListener(new ToggleButtonOnCheckChange());
    }

    private class ToggleButtonOnCheckChange implements CompoundButton.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                m_ImageLight.setImageResource(R.drawable.off_light);
                if (m_Switch.isChecked()) {
                    //當點擊黑夜模式時,關燈後背景爲黃色
                    m_Linear.setBackgroundColor(Color.rgb(255, 255, 0));
                }
                else
                {
                    //當點擊黑夜模式時,關燈後的背景爲黑色
                    m_Linear.setBackgroundColor(Color.rgb(0, 0, 0));
                }
            } else {
                m_ImageLight.setImageResource(R.drawable.on_light);
                m_Linear.setBackgroundColor(Color.rgb(255, 255, 255));
            }
        }
    }
}

運行結果

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-dqyD1VNh-1575802526968)(./light運行效果.png)]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章