android基本控制項學習-----RadioButton&CheckBox,android基本控制項

來源:互聯網
上載者:User

android基本控制項學習-----RadioButton&CheckBox,android基本控制項

RadioButton(單選框)和CheckBox(複選框)講解:

一、基本用法和事件處理

(1)RadioButton單選框,就是只能選擇其中的一個,我們在使用的時候需要將RadioButton放到RadioGroup中使用,同時我們還可以在RadioGroup中設定  orientation屬性來控制單選框的方向。

<?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:background="#ffffff">   <TextView       android:id="@+id/text1"       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:text="請選擇你的性別"       android:textStyle="bold"       android:textSize="30sp"/>    <RadioGroup        android:id="@+id/rg1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <RadioButton            android:id="@+id/rb1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="男"/>        <RadioButton            android:id="@+id/rb2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="女"/>    </RadioGroup>    <Button        android:id="@+id/btn1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="提交"/></LinearLayout>

(2)我們如何擷取選項按鈕選中的值呢,這裡有兩種方法

a:為RadioGroup(radioButton)設定setonCheckChangeListener

package com.example.test3;import android.app.Activity;import android.os.Bundle;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class MainActivity extends Activity {    private RadioGroup radioGroup;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        radioGroup = (RadioGroup) findViewById(R.id.rg1);        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup radioGroup, int checkId) {                RadioButton radioButton = (RadioButton) findViewById(checkId);                Toast.makeText(MainActivity.this,"你選中了" + radioButton.getText().toString(),Toast.LENGTH_LONG).show();            }        });    }}

b:為RadioGroup設定setOnClickListener,但是在使用這個方法的時候需要對RadioGroup內的每一個id

package com.example.test3;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 MainActivity extends Activity {    private RadioGroup radioGroup;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        radioGroup = (RadioGroup) findViewById(R.id.rg1);        Button btn = (Button) findViewById(R.id.btn1);        btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                for (int i = 0; i < radioGroup.getChildCount(); i++) {                    RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);                    if (radioButton.isChecked()) {                        Toast.makeText(MainActivity.this, "你選擇了" + radioButton.getText(), Toast.LENGTH_LONG).show();                        break;                    }                }            }        });    }}

(3)CheckedButto和RadioButton差不多就不多說,直接看代碼吧

<?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:background="#ffffff">   <TextView       android:id="@+id/text1"       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:text="請選擇你的喜歡的水果(可以多選)"       android:textStyle="bold"       android:textSize="22sp"/>   <CheckBox       android:id="@+id/cb1"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="蘋果"/>    <CheckBox        android:id="@+id/cb2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="香蕉"/>    <CheckBox        android:id="@+id/cb3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="梨子"/>    <Button        android:id="@+id/btn1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="提交"/></LinearLayout>
package com.example.test3;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.Toast;public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener,View.OnClickListener{    private CheckBox checkBox1;    private CheckBox checkBox2;    private CheckBox checkBox3;    private Button btn1;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        checkBox1 = (CheckBox) findViewById(R.id.cb1);        checkBox2 = (CheckBox) findViewById(R.id.cb2);        checkBox3 = (CheckBox) findViewById(R.id.cb3);        btn1 = (Button) findViewById(R.id.btn1);        checkBox1.setOnCheckedChangeListener(this);        checkBox2.setOnCheckedChangeListener(this);        checkBox3.setOnCheckedChangeListener(this);        btn1.setOnClickListener(this);    }    @Override    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {        if (compoundButton.isChecked()){            Toast.makeText(MainActivity.this,"你選中了" + compoundButton.getText(),Toast.LENGTH_LONG).show();        }    }    @Override    public void onClick(View view) {        String choose = "";        if(checkBox1.isChecked()){           choose += checkBox1.getText().toString();        }        if(checkBox2.isChecked()){            choose += checkBox2.getText().toString();        }        if(checkBox3.isChecked()){           choose += checkBox3.getText().toString();        }        Toast.makeText(MainActivity.this,"你選中了" + choose,Toast.LENGTH_LONG).show();    }}

二、自訂點擊的效果或者說是點擊框的自訂(以checkBox為例)

一共有兩種方法,但是兩種方法的本質還是一樣的,在兩種方法之後一併附上

(1)第一種:方法簡單和前面講的Button一樣的

定義StateListDrawable檔案

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_checked="true"          android:state_enabled="true"          android:drawable="@mipmap/btn_radio_on"/>    <item android:state_checked="false"          android:state_enabled="true"          android:drawable="@mipmap/btn_radio_off"/></selector>

在布局檔案使用button屬性即可

(2)自訂style

第一步:還是先定義StateListDrawable檔案,上面已經有了

第二步:在style檔案定義自訂的樣式

第三步:在布局檔案中使用style

:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.