实验五 对话框的使用
一、实验要求和目的
package com.example.shiyan5;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button bt2;
private Button bt3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt2 = findViewById(R.id.bt2);
bt3 = findViewById(R.id.bt3);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showNormalDialog();
}
});
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showInputDialog();
}
});
}
private void showNormalDialog() {
final AlertDialog.Builder normalDialog = new AlertDialog.Builder(MainActivity.this);
normalDialog.setIcon(R.drawable.hat);
normalDialog.setTitle("退出");
normalDialog.setMessage("确定要退出码");
normalDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//...To-do
}
});
normalDialog.setNegativeButton("关闭",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//...To-do
}
});
// 显示
normalDialog.show();
}
private void showInputDialog() {
/*@setView 装入一个EditView
*/
final EditText editText = new EditText(MainActivity.this);
AlertDialog.Builder inputDialog =
new AlertDialog.Builder(MainActivity.this);
inputDialog.setIcon(R.drawable.alert);
inputDialog.setTitle("注册").setView(editText);
inputDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,
editText.getText().toString(),
Toast.LENGTH_SHORT).show();
}
}).show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
>
<ImageView
android:id="@+id/qq"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/qq"
android:scaleType="fitCenter"
android:background="#ffffff"
></ImageView>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/qq"
android:layout_marginLeft="15dp"
android:text="用户名:"
android:textSize="20sp"
></TextView>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et1"
android:hint="请输入用户名"
android:background="@drawable/editext_selector"
android:layout_toRightOf="@+id/tv1"
android:textSize="20sp"
></EditText>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/qq"
android:layout_below="@id/tv1"
android:layout_marginLeft="15dp"
android:text="密码:"
android:textSize="20sp"
android:layout_marginTop="30dp"
></TextView>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et2"
android:hint="请输入密码"
android:background="@drawable/editext_selector"
android:layout_toRightOf="@+id/tv1"
android:layout_below="@id/et1"
android:textSize="20sp"
android:layout_marginTop="30dp"
android:inputType="textPassword"
></EditText>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/qq"
android:text="记住密码"
android:id="@+id/ck1"
android:layout_marginLeft="100dp"
></CheckBox>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/qq"
android:text="自动登录"
android:layout_toRightOf="@+id/ck1"
android:id="@+id/ck2"
android:layout_marginLeft="20dp"
></CheckBox>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="登录"
android:layout_below="@+id/ck1"
android:layout_marginTop="30dp"
android:layout_marginLeft="100dp"
android:textSize="18sp"
></Button>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="退出"
android:layout_below="@+id/ck1"
android:layout_marginTop="30dp"
android:layout_marginLeft="100dp"
android:textSize="18sp"
android:layout_toRightOf="@+id/bt1"
></Button>
<Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="快速注册"
android:layout_below="@+id/bt1"
android:layout_marginTop="30dp"
android:layout_marginLeft="100dp"
android:textSize="18sp"
android:background="@drawable/shape_edit_normal"
android:textColor="#00F5FF"
></Button>
<Button
android:id="@+id/bt4"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="忘记密码"
android:layout_below="@+id/bt1"
android:layout_marginTop="30dp"
android:layout_marginLeft="100dp"
android:textSize="18sp"
android:layout_toRightOf="@+id/bt1"
android:background="@drawable/shape_edit_normal"
android:textColor="#00F5FF"
></Button>
</RelativeLayout>