package com.example.question;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.AlteredCharSequence;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=findViewById(R.id.bt);
rg=findViewById(R.id.rg);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i=0;i<rg.getChildCount();i++){
RadioButton radioButton=(RadioButton)rg.getChildAt(i);
if(radioButton.isChecked()){
if(radioButton.getText().equals("B:100")){
Toast.makeText(MainActivity.this, "回答正确", Toast.LENGTH_SHORT).show();
}else{
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setMessage("挖槽就覅我仿佛哈方脑壳祭敖包VS加菲hi然后我if无法if独守空房反馈的少年开发你上课内存空间房间爱附件附件救死扶伤大家啊克拉克三年");
builder.setPositiveButton("确定",null).show();
}
break;
}
}
}
});
}
}
以上是java代码
下面这个是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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hfdhsahfdsahdhscsvvsihaishivhwiahpvshclkslknckdslnclknsdcncncndsncckndsknccdsc
cdnskncckdnscclknsclncnx nnnvnnNsdvpwo[fg[jjwogjamav帮忙发快递你看你放开那可能男方覅热痱粉热热FM二饭
每次破产配件费票据热得快吧梵蒂冈开发的骄傲公检法看看俊男!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:layout_width="wrap_content"
android:id="@+id/rb_a"
android:text="A:125"
android:layout_height="wrap_content"/>
<RadioButton
android:layout_width="wrap_content"
android:id="@+id/rb_b"
android:text="B:100"
android:layout_height="wrap_content"/>
<RadioButton
android:layout_width="wrap_content"
android:id="@+id/rb_c"
android:text="C:175"
android:layout_height="wrap_content"/>
<RadioButton
android:layout_width="wrap_content"
android:id="@+id/rb_d"
android:text="D:200"
android:layout_height="wrap_content"/>
</RadioGroup>
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"/>
</LinearLayout>
结果显示图为
package com.example.deme;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
CheckBox like1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
like1=findViewById(R.id.like1);
like1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(like1.isChecked()){
Toast.makeText(MainActivity.this, like1.getText(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
<?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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<CheckBox
android:id="@+id/like1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="体育"
/>
<CheckBox
android:id="@+id/like2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="美术"
/>
<CheckBox
android:id="@+id/like3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="音乐"
/>
</LinearLayout>
效果图:
<?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">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录后该应用将获得以下授权!"
android:textSize="14dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获得你的公开信息(昵称,头像)"
android:textColor="#2061E0"
android:textSize="12sp"
android:checked="true"
android:id="@+id/checkbox1"
/>
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="寻找与你共同使用该应用的好友"
android:textColor="#248BD1"
android:textSize="12sp"
android:checked="true"
/>
<CheckBox
android:id="@+id/checkbox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="帮助你通过该应用发送消息"
android:textColor="#0D30E2"
android:textSize="12sp"
android:checked="true"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_login"
android:background="#DBE20E"
android:layout_margin="20dp"
android:text="确认登录"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DBE20E"
android:layout_margin="20dp"
android:text="取消"/>
</LinearLayout>
package com.example.abcde;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btn_login;
CheckBox checkBox1,checkBox2,checkBox3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_login=findViewById(R.id.btn_login);
checkBox1=findViewById(R.id.checkbox1);
checkBox2=findViewById(R.id.checkbox2);
checkBox3=findViewById(R.id.checkbox3);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String checked="";
if(checkBox1.isChecked()){
checked+=checkBox1.getText().toString();
}
if(checkBox2.isChecked()){
checked+=checkBox2.getText().toString();
}
if (checkBox3.isChecked()){
checked+=checkBox3.getText().toString();
}
Toast.makeText(MainActivity.this, checked, Toast.LENGTH_LONG).show();
}
});
}
}
上面都是复选框的实例