Android——RadioGroup和CheckBox

來源:互聯網
上載者:User

標籤:

xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.chenshuai.test322.UIActivity">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="請選擇Android的開發語言是什嗎?"        android:padding="10dp"/>    <RadioGroup        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:id="@+id/rg">        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="C++"            android:id="@+id/rb1"            android:layout_marginRight="30dp"            android:checked="true"            />        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="C"            android:id="@+id/rb2"            android:layout_marginRight="30dp"            />        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="JAVA"            android:id="@+id/rb3"            android:layout_marginRight="30dp"            />        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="C#"            android:id="@+id/rb4"            />    </RadioGroup>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="請選擇字型效果:"        android:id="@+id/ziti"/>    <CheckBox        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="宋體"        android:checked="true"        android:id="@+id/cb_song"/>    <CheckBox        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="加粗"        android:id="@+id/cb_cu"/>    <CheckBox        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="斜體"        android:id="@+id/cb_xie"/>    <CheckBox        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="底線"        android:id="@+id/cb_xia"/></LinearLayout>

java

package com.example.chenshuai.test322;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class UIActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_ui);        RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rg);        //radiogroup的監聽事件 匿名內部類        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup group, int checkedId) {                RadioButton rb = (RadioButton) findViewById(checkedId);                if (checkedId == R.id.rb3) {                    Toast.makeText(UIActivity.this, "選對了", Toast.LENGTH_LONG).show();                }                Toast.makeText(UIActivity.this, rb.getText(), Toast.LENGTH_LONG).show();            }        });        CheckBox cb_song = (CheckBox)findViewById(R.id.cb_song);        cb_song.setOnCheckedChangeListener(new cboncheckedchangelistener());        CheckBox cb_cu = (CheckBox)findViewById(R.id.cb_cu);        cb_cu.setOnCheckedChangeListener(new cboncheckedchangelistener());        CheckBox cb_xia = (CheckBox)findViewById(R.id.cb_xia);        cb_xia.setOnCheckedChangeListener(new cboncheckedchangelistener());        CheckBox cb_xie = (CheckBox)findViewById(R.id.cb_xie);        cb_xie.setOnCheckedChangeListener(new cboncheckedchangelistener());    }        //checkbox的監聽事件 內部類    private class cboncheckedchangelistener implements CompoundButton.OnCheckedChangeListener    {        @Override        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {            CheckBox cb = (CheckBox)buttonView;                        if (isChecked)            {                Toast.makeText(UIActivity.this, "選中了"+cb.getText(), Toast.LENGTH_SHORT).show();            }            else            {                Toast.makeText(UIActivity.this, "取消選中了"+cb.getText(), Toast.LENGTH_SHORT).show();            }        }    }}

Android——RadioGroup和CheckBox

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.