麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > Android > 正文

android RadioButton和CheckBox組件的使用方法

2020-04-11 12:00:49
字體:
來源:轉載
供稿:網友

RadioButton是單選按鈕,多個RadioButton放在一個RadioGroup控件中,也就是說每次只能有1個RadioButton被選中。而CheckBox是多選按鈕,Toatst是android中帶的一個用于顯示提示小窗口消息的控件,其提示的內容過一會兒會自動消失。
RadioGroup和CheckBox控件設置監聽器都是用的setOnCheckedChangeListener函數,其輸入參數是一個函數,且函數內部要實現1個內部類。RadioGroup監聽器的輸入參數用的是RadioGroup.OnCheckedChangeListener(),而CheckBox監聽器的輸入參數用的是函數CompoundButton.OnCheckedChangeListener().
開發環境:android4.1

實驗效果如下(采用的是線性布局):

效果圖:


上面3個為一組RadioGroup,每選中其中一個RadioButton,則會有相應的提示。且只能選中其中的一個。
下面的4都為CheckBox,可以選中其中的多個。每個CheckBox被選中或者取消選中都有相應的文字提示小窗口。

代碼如下:
MainActivity.java:

復制代碼 代碼如下:

package com.example.control1;

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

public class MainActivity extends Activity {

    //定義各控件的變量
    private TextView who = null;
    private TextView how = null;
    private RadioGroup who_group = null;
    private RadioButton china = null;
    private RadioButton america = null;
    private RadioButton others = null;
    private CheckBox less = null;
    private CheckBox thirty = null;
    private CheckBox forty = null;
    private CheckBox fifty = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //獲得對應的控件
        who = (TextView)findViewById(R.id.who);
        how = (TextView)findViewById(R.id.how);
        who_group = (RadioGroup)findViewById(R.id.who_group);
        china = (RadioButton)findViewById(R.id.china);
        america = (RadioButton)findViewById(R.id.america);
        others = (RadioButton)findViewById(R.id.others);
        less = (CheckBox)findViewById(R.id.less);
        thirty = (CheckBox)findViewById(R.id.thirty);
        forty = (CheckBox)findViewById(R.id.forty);
        fifty = (CheckBox)findViewById(R.id.fifty);

        //設置who_group的監聽器,其實是一句代碼,其參數是一個帶有重構函數的對象
        who_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

   public void onCheckedChanged(RadioGroup group, int checkedId) {
       // TODO Auto-generated method stub
       if(checkedId == china.getId()){
  Toast.makeText(MainActivity.this,"中國", Toast.LENGTH_SHORT).show();
       }
       else if(checkedId == america.getId()){
  Toast.makeText(MainActivity.this, "美國", Toast.LENGTH_SHORT).show();
       }
       else if(checkedId == others.getId()){
  Toast.makeText(MainActivity.this, "其它國家", Toast.LENGTH_SHORT).show();
       }
   }
        });

        //下面為4個checkbox多選按鈕分別建立監聽器
        less.setOnCheckedChangeListener(new OnCheckedChangeListener() {

   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
       // TODO Auto-generated method stub
       if(isChecked)
       {
  Toast.makeText(MainActivity.this, "30個以下", Toast.LENGTH_SHORT).show();
       }
       else{
  Toast.makeText(MainActivity.this, "不是30個以下", Toast.LENGTH_SHORT).show();
       }
   }
        });

       
      //下面為4個checkbox多選按鈕分別建立監聽器
        thirty.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
       // TODO Auto-generated method stub
       if(isChecked)
       {
  Toast.makeText(MainActivity.this, "30~39", Toast.LENGTH_SHORT).show();
       }
       else{
  Toast.makeText(MainActivity.this, "不是30~39", Toast.LENGTH_SHORT).show();
       }
   }
        });

      //下面為4個checkbox多選按鈕分別建立監聽器
        forty.setOnCheckedChangeListener(new OnCheckedChangeListener() {

   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
       // TODO Auto-generated method stub
       if(isChecked)
       {
  Toast.makeText(MainActivity.this, "40~49", Toast.LENGTH_SHORT).show();
       }
       else{
  Toast.makeText(MainActivity.this, "不是40~49", Toast.LENGTH_SHORT).show();
       }
   }
        });

      //下面為4個checkbox多選按鈕分別建立監聽器
        fifty.setOnCheckedChangeListener(new OnCheckedChangeListener() {

   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
       // TODO Auto-generated method stub
       if(isChecked)
       {
  Toast.makeText(MainActivity.this, "50以上", Toast.LENGTH_SHORT).show();
       }
       else{
  Toast.makeText(MainActivity.this, "不是50以上", Toast.LENGTH_SHORT).show();
       }
   }
        });

   

    }
}



activity_main.xml:
復制代碼 代碼如下:

<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"
    >
    <TextView
        android:id="@+id/who"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/who"
/>
    <RadioGroup
        android:id="@+id/who_group"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="vertical"
      >
       <RadioButton
  android:id="@+id/china"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:text="@string/china"
  />
       <RadioButton
  android:id="@+id/america"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/america"
  />
       <RadioButton
  android:id="@+id/others"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/others"
  />
       </RadioGroup>
       <TextView
  android:id="@+id/how"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/how"
  />
        <CheckBox
  android:id="@+id/less"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/less"
  />
       <CheckBox
  android:id="@+id/thirty"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/thirty"
  />
       <CheckBox
  android:id="@+id/forty"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/forty"
  />
       <CheckBox
  android:id="@+id/fifty"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/fifty"
  />

</LinearLayout>



實驗總結:通過本次實驗對RadioGroup,CheckBox,RadioButton和Toast這4個控件的簡單使用有了個初步的了解。

作者:tornadomeet

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美成人精品欧美一级乱黄 | 98国内自拍在线视频 | 国产一区二区三区视频在线 | 国产精品一品二区三区四区18 | 国产免费一区二区三区 | 小视频免费在线观看 | 一级毛片在线免费观看 | 性爱在线免费视频 | 国产毛片在线 | 中文字幕在线看第二 | 亚洲国产精品久久久久婷婷老年 | 毛片免费视频播放 | fc2国产成人免费视频 | 久久亚洲网 | 一级毛片在线视频 | 男女无套免费视频 | 深夜视频福利 | 日韩中文字幕三区 | 欧美成人视 | 成人午夜在线播放 | 久久手机在线视频 | 秋霞a级毛片在线看 | 中文字幕精品一二三四五六七八 | 7777网站 | 久久久免费观看完整版 | 天堂在线中文资源 | 龙床上的呻吟高h | 国产精品高清一区 | 欧美精品一区二区三区在线 | 国产在线观看91一区二区三区 | 亚洲人成中文字幕在线观看 | 成人福利视频网站 | 亚洲卡通动漫在线观看 | 在线观看免费视频麻豆 | 欧美激情天堂 | 美国黄色毛片女人性生活片 | 国产免费小视频在线观看 | 亚欧在线免费观看 | 羞羞视频免费网站日本动漫 | 国产人成免费爽爽爽视频 | 国产在线观看一区二区三区 |