android studio版本:Android Studio Chipmunk | 2021.2.1 Patch 2
例程名称:radiobutton1
方法一:点击立即跳转
1、新建一个empty activity. 工程名:RadioButton1.
2、activity_main.xml添加一个radiogroup, 在radiogroup下面添加两个radiobutton.如图:
3、新建两个activity.方法如图,名字分别为ActivityA.java和ActivityB.java.两个activity里面只有一个textview,区分是哪个activity就可以了。
4、radgroup设置监听.setOnCheckedChangeListener(){},并重写点击事件onCheckedChanged。if判断点击的是哪个单选按钮实现跳转。
radgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int checkID=group.getCheckedRadioButtonId();
if (checkID==R.id.radioButton1){
Intent intent = new Intent();
intent.setClass(MainActivity.this,ActivityA.class);
startActivity(intent);
System.out.println("ID值="+group.getCheckedRadioButtonId());
finish();
}else{
Intent intent = new Intent();
intent.setClass(MainActivity.this,ActivityB.class);
startActivity(intent);
System.out.println("ID值="+group.getCheckedRadioButtonId());
finish();
}
}
});
注意:int checkID=group.getCheckedRadioButtonId();里面,checkID是个整数,应该是系统分配。group.getCheckedRadioButtonId()可获取,具体值不用管。
跳转代码:
Intent intent = new Intent(); intent.setClass(MainActivity.this,ActivityA.class);//动作:从主页面跳到activityA页面。 startActivity(intent);//开始跳转 System.out.println("ID值="+group.getCheckedRadioButtonId());//显示ID值 finish();//销毁本页面
动图展示:
全部代码:
mainactivity.java(关键)
package com.example.radiobutton1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity {
private RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);
//第一种获得单选按钮值的方法
//为radioGroup设置一个监听器:setOnCheckedChanged()
radgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int checkID=group.getCheckedRadioButtonId();
if (checkID==R.id.radioButton1){
Intent intent = new Intent();
intent.setClass(MainActivity.this,ActivityA.class);//动作:从主页面跳到activityA页面。
startActivity(intent);//开始跳转
System.out.println("ID值="+group.getCheckedRadioButtonId());//显示ID值
finish();//销毁本页面
}else{
Intent intent = new Intent();
intent.setClass(MainActivity.this,ActivityB.class);
startActivity(intent);
System.out.println("ID值="+group.getCheckedRadioButtonId());
finish();
}
}
});
}
}
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">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="界面A"
android:textSize="30sp" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="界面B"
android:textSize="30sp" />
</RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
activitya.java和activityb.java无新增代码,都是默认。
界面A的xml代码(我的系统里是显示的是activity_activity.xml),界面B只要把textview复制过去改成“界面B”即可,不再贴代码:
<?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=".ActivityA">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是界面A"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
方法二:点击按钮后跳转
在activity_main.xml里面添加一个按钮,监听radiobutton的点击情况,点击后实现跳转。相对于上例,只有主页面和代码有变化,其他完全不变。
动图展示:
全部代码:(只有mainactivity.java和activity_main.xml变化,其他不贴)
mainactivity.java
package com.example.radiobutton1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity {
private RadioGroup radioGroup;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);
// //方法一:
// //为radioGroup设置一个监听器:setOnCheckedChanged()
// radgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(RadioGroup group, int checkedId) {
// int checkID=group.getCheckedRadioButtonId();
// if (checkID==R.id.radioButton1){
// Intent intent = new Intent();
// intent.setClass(MainActivity.this,ActivityA.class);//动作:从主页面跳到activityA页面。
// startActivity(intent);//开始跳转
// System.out.println("ID值="+group.getCheckedRadioButtonId());//显示ID值
// finish();//销毁本页面
// }else{
// Intent intent = new Intent();
// intent.setClass(MainActivity.this,ActivityB.class);
// startActivity(intent);
// System.out.println("ID值="+group.getCheckedRadioButtonId());
// finish();
// }
//
// }
//
// });
//方法二:
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int checkID=radgroup.getCheckedRadioButtonId();
if (checkID==R.id.radioButton1){
Intent intent = new Intent();
intent.setClass(MainActivity.this,ActivityA.class);//动作:从主页面跳到activityA页面。
startActivity(intent);//开始跳转
finish();//销毁本页面
}else{
Intent intent = new Intent();
intent.setClass(MainActivity.this,ActivityB.class);
startActivity(intent);
finish();
}
}
});
}
}
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">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="界面A"
android:textSize="30sp" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="界面B"
android:textSize="30sp" />
</RadioGroup>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击我跳转"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline2" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="233dp" />
</androidx.constraintlayout.widget.ConstraintLayout>