Android Studio 实现性别单选对话框

效果图:

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="match_parent"
        android:orientation="horizontal"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="2dp"
            android:layout_height="63dp"
            android:layout_weight="1"
            android:text="性别"
            android:textAllCaps="false"
            android:textSize="24sp" />

        <RadioGroup
            android:id="@+id/radiogroup"
            android:layout_width="129dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/radioButton6"
                android:layout_width="93dp"
                android:layout_height="65dp"
                android:text="男"
                android:textSize="24sp" />

            <RadioButton
                android:id="@+id/radioButton5"
                android:layout_width="95dp"
                android:layout_height="66dp"
                android:text="女"
                android:textSize="24sp" />
        </RadioGroup>

        <Button
            android:id="@+id/button6"
            android:layout_width="111dp"
            android:layout_height="67dp"
            android:layout_weight="1"
            android:text="提交"
            android:textSize="24sp" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java:
 

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RadioGroup sex=(RadioGroup)findViewById(R.id.radiogroup);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkelId) {
                RadioButton r=(RadioButton) findViewById(checkelId);
                Toast toast=Toast.makeText(MainActivity.this,"选择是"+r.getText(),Toast.LENGTH_LONG);
                toast.show();
            }
        });
    }
}

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android Studio实现单选对话框或多选对话框,可以使用AlertDialog.Builder类。下面是实现单选对话框和多选对话框的代码示例: 单选对话框: ```java AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("选择项"); builder.setSingleChoiceItems(items, checkedItem, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 处理选中项的逻辑 } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 处理确定按钮点击事件的逻辑 } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 处理取消按钮点击事件的逻辑 } }); AlertDialog dialog = builder.create(); dialog.show(); ``` 多选对话框: ```java AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("选择项"); builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // 处理选中项的逻辑 } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 处理确定按钮点击事件的逻辑 } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 处理取消按钮点击事件的逻辑 } }); AlertDialog dialog = builder.create(); dialog.show(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值