目录
概述
一、环境
二、代码
三、运行结果
四、总结
概述
? ? ? ? 项目需要,在此编写一篇关于单选按键的博文,分别有两种方法实现单选功能,废话少说,直接进入主题。
一、环境
开发环境(IDE):Android Studio 2021.1.1版本 运行环境:华为手机
二、代码
1)、activity_main.xml布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp"
tools:ignore="MissingConstraints">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:text="声音与振动" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="振动强度:" />
<RadioGroup
android:id="@+id/radiogroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:orientation="vertical">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="等级1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="等级2" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="等级3" />
</RadioGroup>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="声音设置:" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="响铃" />
<RadioButton
android:id="@+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="静音" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
2)、布局效果图
3)、MainActivity.java
package com.example.mytest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private RadioGroup radioGroup;
private RadioButton radioButton1,radioButton2,radioButton3;
private Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init_View();
}
public void init_View(){
/* 方法1:
* radioGroup
* */
radioGroup = (RadioGroup)findViewById(R.id.radiogroup1);
// radioButton1 = (RadioButton)findViewById(R.id.radioButton1);
// radioButton2 = (RadioButton)findViewById(R.id.radioButton2);
// radioButton3 = (RadioButton)findViewById(R.id.radioButton3);
View radioButton4 = findViewById(R.id.radioButton4);
View radioButton5 = findViewById(R.id.radioButton5);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
View checkView = group.findViewById(checkedId);
if (!checkView.isPressed()) {
return;
}
switch (checkedId) {
case R.id.radioButton1:
System.out.println("click radioButton1");
toast = Toast.makeText(getApplicationContext(),"等级1",Toast.LENGTH_SHORT);
// //toast.setGravity(Gravity.CENTER,0,0);
toast.show();
break;
case R.id.radioButton2:
System.out.println("click radioButton2");
toast = Toast.makeText(getApplicationContext(),"等级2",Toast.LENGTH_SHORT);
toast.show();
break;
case R.id.radioButton3:
System.out.println("click radioButton3");
toast = Toast.makeText(getApplicationContext(),"等级3",Toast.LENGTH_SHORT);
toast.show();
break;
default:
break;
}
}
});
/* 方法2:
* radioButton
* */
radioButton4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(radioButton4.isClickable()) {
mySetCheckedStateForView(R.id.radioButton5,false);
System.out.println("click radioButton4");
toast = Toast.makeText(getApplicationContext(),"响铃",Toast.LENGTH_SHORT);
toast.show();
}
}
});
radioButton5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(radioButton5.isClickable()) {
mySetCheckedStateForView(R.id.radioButton4,false);
System.out.println("click radioButton5");
toast = Toast.makeText(getApplicationContext(),"静音",Toast.LENGTH_SHORT);
toast.show();
}
}
});
}
private void mySetCheckedStateForView(int viewId, boolean checked) {
View checkedView = findViewById(viewId);
if (checkedView != null && checkedView instanceof RadioButton) {
((RadioButton) checkedView).setChecked(checked);
}
}
}
三、运行结果
四、总结
? ? ? ? 好久没写Android,生疏不少,有空再继续更新Android的文章,谢谢各位看官赏阅^_^!!!
|