Android学习笔记篇2. 单选按钮、复选按钮
生活随笔
收集整理的這篇文章主要介紹了
Android学习笔记篇2. 单选按钮、复选按钮
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
單選按鈕
在XML里寫一組單選按鈕(2個)+ 一個文本(用于提示):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><RadioGroupandroid:id="@+id/rdg"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><RadioButtonandroid:id="@+id/rbtn"android:textSize="25dp"android:text="男"android:layout_width="wrap_content"android:layout_height="wrap_content"/><RadioButtonandroid:textSize="25dp"android:text="女"android:layout_width="wrap_content"android:layout_height="wrap_content"/></RadioGroup><TextViewandroid:id="@+id/tv"android:textSize="30dp"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>在Java中編寫代碼
package com.example.no_2;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle; import android.widget.RadioGroup; import android.widget.TextView;public class MainActivity extends AppCompatActivity {private RadioGroup radioGroup;private TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);radioGroup = (RadioGroup) findViewById(R.id.rdg);textView = (TextView) findViewById(R.id.tv);/*** 利用setOnCheckedChangeListtener()為RadioGroup設置監聽事件*/radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup radioGroup, int i) {// 判斷被點擊的RadioButtonif (i == R.id.rbtn) {textView.setText("您的性別是:男");} else {textView.setText("您的性別是:女");}}});} }運行,點擊按鈕,在底下提示相應信息:
復選按鈕
在XML里寫一組復選按鈕 + 兩個提示文本 + 一個顯示輸出文本
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="請選擇愛好:"android:textSize="18dp"/><CheckBoxandroid:id="@+id/shuttlecock"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="羽毛球"android:textSize="18dp"/><CheckBoxandroid:id="@+id/basketball"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="籃球"android:textSize="18dp"/><CheckBoxandroid:id="@+id/pingpong"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="乒乓球"android:textSize="18dp"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="您選擇的興趣愛好為:"android:textSize="22dp"/><TextViewandroid:id="@+id/hobby"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="18dp"/></LinearLayout>在Java中編寫代碼,利用TextView提示哪一個復選框被選擇:
package com.example.no_2;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioGroup; import android.widget.TextView;public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {private TextView hobby;private String hobbys;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);/*** 初始化CheckBox控件*/CheckBox shuttlecock = (CheckBox) findViewById(R.id.shuttlecock);CheckBox basketball = (CheckBox) findViewById(R.id.basketball);CheckBox pingpong = (CheckBox) findViewById(R.id.pingpong);shuttlecock.setOnCheckedChangeListener(this);basketball.setOnCheckedChangeListener(this);pingpong.setOnCheckedChangeListener(this);hobby = (TextView) findViewById(R.id.hobby);hobbys = new String();}@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {String motion = compoundButton.getText().toString();/*** 簡易算法控制提示字符串*/if (b) {if (!hobbys.contains(motion)) {hobbys = hobbys + motion;hobby.setText(hobbys);}} else {if (hobbys.contains(motion)) {hobbys = hobbys.replace(motion, "");hobby.setText(hobbys);}}} }運行,在底下提示對應信息:
總結
以上是生活随笔為你收集整理的Android学习笔记篇2. 单选按钮、复选按钮的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android学习笔记篇1. 从按钮的点
- 下一篇: web消息推送的各种解决办法