AlertDialog

65 阅读1分钟

AlertDialog详解:

普通对话框:

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"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="性别统计"
        android:textColor="#2196F3"
        android:textSize="24dp"
        android:gravity="center"
        android:textStyle="bold"
      android:layout_margin="5dp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请选择您的性别"
        android:textColor="@color/black"
        android:textSize="24dp"
        />
    <RadioGroup
        android:id="@+id/gender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/rbmale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:textSize="24dp"/>
            <RadioButton
                android:id="@+id/rbfemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:textSize="24dp"/>
         </RadioGroup>
    <TextView
        android:id="@+id/tv_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       android:hint="您的性别是:"
        android:textColor="@color/black"
        android:textSize="24dp"
        />
</LinearLayout>

java:

package com.example.radio;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private RadioGroup mRgGender;
private TextView mTvInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

initView();
mRgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        if(checkedId==R.id.rbmale)
        {
            mTvInfo.setText("性别:男");
        }else{
            mTvInfo.setText("性别:女");
        }

    }
});
        Toast.makeText(MainActivity.this,"请如实选择",Toast.LENGTH_LONG).show();
    }
    private void initView(){
    mRgGender=findViewById(R.id.gender);
    mTvInfo=findViewById(R.id.tv_info);
    }
public void onBackPressed()
{
    AlertDialog dialog;
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("普通对话框").setIcon(R.drawable.ic_launcher_background)
        .setMessage("您是否确定退出当前应用?")
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            finish();//退出当前页面
            }
        })
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
        dialog.dismiss();//取消掉对话框
            }
        });
dialog =builder.create();
dialog.show();

}

}

效果图:

image.png