3-5
MainActivity.java
import android.app.AlertDialog;
import android.app.ProgressDialog;
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.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);
@Override
public void onClick(View arg0)
{
if(arg0 == btn1)
{
//设置对话框的标题
dialog.setTitle("警告");
//设置对话框的图标
//设置对话框显示的内容
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.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.paswdEdit);
//登录验证
//取出输入编辑框的值与密码“aaa”比较
if((txt.getText().toString()).equals("aaa"))
Toast.makeText(getApplicationContext(),
"登录成功", Toast.LENGTH_SHORT).show();
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();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
>
<Button
android:id="@+id/button1"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="打开普通对话框"
android:layout_gravity="center_horizontal" />
<Button
android:id="@+id/button2"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="打开输入对话框"
android:layout_gravity="center_horizontal" />
</LinearLayout>
login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:id="@+id/user"
android:textSize="18sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:id="@+id/textView"
android:textSize="18sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/paswdEdit" />
</LinearLayout>
3-6
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="com.example.gaza.ex3_6.MainActivity"
android:weightSum="1">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开进度对话框"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开日期对话框"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开时间对话框"/>
<TextView
android:id="@+id/text"
android:layout_width="133dp"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="0.22" />
</LinearLayout>
MainActivity.java
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends Activity
{
String str1 ="";
String str2 ="";
Button btn1,btn2,btn3;
TextView text;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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());
text = (TextView)findViewById(R.id.text);
}
class mClick implements OnClickListener
{
int m_year = 2012;
int m_month = 1;
int m_day = 1;
int m_hour = 12, m_minute = 1;
@Override
public void onClick(View v)
{
if(v == 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(v == btn2)
{
//设置日期监听器
OnDateSetListener dateListener = new OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth)
{
m_year = year;
m_month = monthOfYear;
m_day = dayOfMonth;
str1 = "日期:"+m_year+"年"+(m_month+1)+"月"+m_day+"日\n";
text.setText(str1+str2);
}
};
//创建日期对话框对象
DatePickerDialog date = new DatePickerDialog(MainActivity.this,
dateListener, m_year, m_month, m_day);
date.setTitle("日期对话框");
date.show();
}
else if(v == btn3)
{ //设置时间监听器
OnTimeSetListener timeListener = new OnTimeSetListener()
{
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
m_hour = hourOfDay;
m_minute = minute;
str2 = "时间:"+m_hour+":"+m_minute;
text.setText(str1+str2);
}
};
TimePickerDialog d = new TimePickerDialog(MainActivity.this,
timeListener, m_hour, m_minute, true);
d.setTitle("时间对话框");
d.show();
}
}
}
}