1、在用户登录对话中设计了用户登录的布局文件login.xml 供用户输入相关验证信息。
activity.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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:text="打开普通对话框"
android:textSize="20sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:text="打开输入对话框"
android:textSize="20sp"/>
</LinearLayout>
login.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:id="@+id/user"
android:text="用户名"
android:textSize="18sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/userEdit"
android:textSize="18sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/password"
android:text="密码"
android:textSize="18sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/passwdEdit"
android:textSize="18sp"/>
</LinearLayout>
MainActivity.java 源代码
package com.jiancai.example3_5;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ProgressDialog mydialog;
Button btn1,btn2;
LinearLayout login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
btn1.setOnClickListener(new mclick());
btn2.setOnClickListener(new mclick());
}
class mclick implements View.OnClickListener{
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
@SuppressLint("ResourceType")
@Override
public void onClick(View arg0) {
if(arg0==btn1){
dialog.setTitle("警告"); //设置对话的标题
dialog.setIcon(R.drawable.icon1); //设置对话框的图标
dialog.setMessage("本项操作可能导致信息泄露"); //设置对话框显示的内容
dialog.setPositiveButton("确定",new okClick());//设置对话框的"确定"按钮
dialog.create(); //创建对象框
dialog.show(); //显示对话框
}
else if (arg0==btn2){
login=(LinearLayout)getLayoutInflater().inflate(R.layout.login,null);
dialog.setTitle("用户登录").setMessage("请输入用户名和密码").setView(login);
dialog.setPositiveButton("确定",new loginClick());
dialog.setNegativeButton("退出",new exitClick());
dialog.setIcon(R.drawable.icon2);
dialog.create();
dialog.show();
}
}
}
class okClick implements DialogInterface.OnClickListener{
@Override
public void onClick(DialogInterface dialog ,int which) {
dialog.cancel();
}
}
class loginClick implements DialogInterface.OnClickListener{
EditText txt;
@Override
public void onClick(DialogInterface dialog, int which) {
txt=(EditText)login.findViewById(R.id.passwdEdit); //取出输入编辑框的值与密码“admin”比较
//密码为admin时显示登录成功
if (txt.getText().toString().equals("admin")){
Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_SHORT).show();
}
//密码不为admin时为登录失败
else
Toast.makeText(getApplicationContext(),"密码错误",Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
class exitClick implements DialogInterface.OnClickListener{
@Override
public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish(); //点击退出按钮退出程序
}
}
}
结果截图:
2、显示进度及日期、时间对话框
activity_main.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="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/showtime"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/date"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/time"
android:textSize="20sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:text="程序进度"
android:textSize="20sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:text="设置日期"
android:textSize="20sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:text="设置时间"
android:textSize="20sp"/>
</LinearLayout>
MianActivity.java 源代码
package com.jiancai.example3_6;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
TextView text1,text2,text3;
Button btn1,btn2,btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1=(TextView)findViewById(R.id.showtime);
text2=(TextView)findViewById(R.id.date);
text3=(TextView)findViewById(R.id.time);
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
btn3=(Button)findViewById(R.id.button3);
btn1.setOnClickListener(new mclick());
btn2.setOnClickListener(new mclick());
btn3.setOnClickListener(new mclick());
}
class mclick implements View.OnClickListener{
int m_year=2012;
int m_month=1;
int m_day=1;
int m_hour=12;
int m_minute=1;
@Override
public void onClick(View view) {
if (view==btn1){
ProgressDialog d=new ProgressDialog(MainActivity.this);
d.setTitle("进度对话框");
d.setIndeterminate(true); //使进度条来回移动,形成动画效果
d.setMessage("程序正在Loading......");
d.setCancelable(true); //点击外部退出
d.setMax(10);
d.show();
}
else if (view==btn2){
DatePickerDialog.OnDateSetListener dateListener=new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
m_year=year;
m_month=monthOfYear;
m_day=dayOfMonth;
text2.setText("你所选择的日期为:"+year+"/"+(monthOfYear+1)+"/"+dayOfMonth);
}
};
DatePickerDialog date=new DatePickerDialog(MainActivity.this,dateListener,m_year,m_month,m_day);
date.setTitle("日期对话框");
date.show();
}
else if (view==btn3){
TimePickerDialog.OnTimeSetListener timeListener=new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
m_hour=hourOfDay;
m_minute=minute;
text3.setText("你所选择的时间为:"+hourOfDay+":"+minute);
}
};
TimePickerDialog d=new TimePickerDialog(MainActivity.this,timeListener,m_hour,m_minute,true);
d.setTitle("时间对话框");
d.show();
}
}
}
}
结果截图: