显示提示消息的对话框-AlertDialog
package com.example.myapplication;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView show;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = findViewById(R.id.show);
}
public void simple(View source) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("简单对话框")
.setIcon(R.drawable.ic_launcher_background)
.setMessage("对话框的测试内容\n第二行内容");
setPositiveButton(builder);
setNegativeButton(builder).create().show();
}
public AlertDialog.Builder setPositiveButton(AlertDialog.Builder builder) {
return builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("点击了“确认”按钮");
}
});
}
public AlertDialog.Builder setNegativeButton(AlertDialog.Builder builder) {
return builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("单击了“取消”按钮");
}
});
}
}
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示对话框"
android:onClick="simple"
/>
</LinearLayout>
简单列表项对话框
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示对话框"
android:onClick="simple"
/>
</LinearLayout>
package com.example.myapplication;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView show;
private String[]items={"疯狂Java讲义","疯狂Ajax讲义","疯狂Android讲义"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = findViewById(R.id.show);
}
public void simple(View source) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("简单列表项对话框")
.setIcon(R.drawable.ic_launcher_background)
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("你选中了《"+items[which]+"》");
}
});
setPositiveButton(builder);
setNegativeButton(builder).create().show();
}
public AlertDialog.Builder setPositiveButton(AlertDialog.Builder builder) {
return builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("点击了“确认”按钮");
}
});
}
public AlertDialog.Builder setNegativeButton(AlertDialog.Builder builder) {
return builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("单击了“取消”按钮");
}
});
}
}
单选列表对话框
package com.example.myapplication;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView show;
private String[]items={"疯狂Java讲义","疯狂Ajax讲义","疯狂Android讲义"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = findViewById(R.id.show);
}
public void simple(View source) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("单选列表项对话框")
.setIcon(R.drawable.ic_launcher_foreground)
.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("你选中了《"+items[which]+"》");
}
});
setPositiveButton(builder);
setNegativeButton(builder).create().show();
}
public AlertDialog.Builder setPositiveButton(AlertDialog.Builder builder) {
return builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("点击了“确认”按钮");
}
});
}
public AlertDialog.Builder setNegativeButton(AlertDialog.Builder builder) {
return builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("单击了“取消”按钮");
}
});
}
}
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示对话框"
android:onClick="simple"
/>
</LinearLayout>
多选列表项对话框
package com.example.myapplication;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView show;
private String[]items={"疯狂Java讲义","疯狂Ajax讲义","疯狂Android讲义"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = findViewById(R.id.show);
}
public void multiChoice(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("多选列表项对话框")
.setIcon(R.drawable.ic_launcher_foreground)
.setMultiChoiceItems(items,new boolean[]{false,true,false,true},null);
setPositiveButton(builder);
setNegativeButton(builder).create().show();
}
}
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示对话框"
android:onClick="multiChoice"
/>
</LinearLayout>
自定义列表对话框
package com.example.myapplication;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView show;
private String[]items={"疯狂Java讲义","疯狂Ajax讲义","疯狂Android讲义"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = findViewById(R.id.show);
}
public void customList(View source){
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("自定义列表对话框")
.setIcon(R.drawable.ic_launcher_foreground)
.setAdapter(new ArrayAdapter<String>(this,R.layout.labelicon,items),null);
setPositiveButton(builder);
setNegativeButton(builder).create().show();
}
public AlertDialog.Builder setPositiveButton(AlertDialog.Builder builder) {
return builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("点击了“确认”按钮");
}
});
}
public AlertDialog.Builder setNegativeButton(AlertDialog.Builder builder) {
return builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("单击了“取消”按钮");
}
});
}
}
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示对话框"
android:onClick="customList"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10px"
android:shadowColor="#00f"
android:shadowDx="4"
android:shadowDy="4"
android:shadowRadius="2"
android:textSize="24dp" />
自定义View对话框
package com.example.myapplication;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView show;
private String[] items = {"疯狂Java讲义", "疯狂Ajax讲义", "疯狂Android讲义"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = findViewById(R.id.show);
}
public void customView(View source) {
TableLayout loginForm = (TableLayout) getLayoutInflater().inflate(R.layout.login, null);
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher_foreground)
.setTitle("自定义View对话框")
.setView(loginForm)
.setPositiveButton("登录", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create().show();
}
}
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示对话框"
android:onClick="customView"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loginForm"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:text="用户名:"
android:textSize="10pt" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写登录账号"
android:selectAllOnFocus="true" />
</TableRow>
<TableRow>
<TextView
android:text="密码:"
android:textSize="10pt" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写密码"
android:selectAllOnFocus="true" />
</TableRow>
<TableRow>
<TextView
android:text="电话号码:"
android:textSize="10pt" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写您的电话号码"
android:selectAllOnFocus="true"
android:phoneNumber="true"/>
</TableRow>
</TableLayout>
对话框风格的窗口
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:theme="@android:style/Theme.Material.Dialog">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:src="@drawable/ic_launcher_background"
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示对话框"
android:onClick="customView"
/>
</LinearLayout>
其他代码同上一案例
使用PopupWindow
package com.example.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View root = this.getLayoutInflater().inflate(R.layout.login, null);
final PopupWindow popupWindow = new PopupWindow(root, 560, 720);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.showAtLocation(findViewById(R.id.button), Gravity.CENTER, 20, 20);
}
});
findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
}
}
使用ProgressDialog创建进度对话框
package com.example.myapplication;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
final static int MAX_PROGRESS = 100;
private int[] data = new int[500];
int progressStatus = 0;
int hasData = 0;
ProgressDialog pd1, pd2;
Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
pd2.setProgress(progressStatus);
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showSpinner(View source) {
ProgressDialog.show(this, "任务执行中", "任务执行中,请稍等", false, true);
}
public void showIndeteminate(View source) {
pd1 = new ProgressDialog(MainActivity.this);
pd1.setTitle("任务正在执行"); //标题
pd1.setMessage("任务正在在执行中。。。。"); //内容
pd1.setCancelable(true); //能用取消按钮
pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //进度条风格
pd1.setIndeterminate(true);
pd1.show();
}
public void showProgress(View source){
progressStatus=0;
hasData=0;
pd2 = new ProgressDialog(MainActivity.this);
pd2.setMax(MAX_PROGRESS);
pd2.setTitle("任务完成百分比");
pd2.setMessage("耗时任务百分比");
pd2.setCancelable(false);
pd2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd2.setIndeterminate(false);
pd2.show();
new Thread(){
public void run(){
while (progressStatus<MAX_PROGRESS){
progressStatus=MAX_PROGRESS*doWork()/data.length;
handler.sendEmptyMessage(0x123);
}
if(progressStatus>=MAX_PROGRESS){
pd2.dismiss();
}
}
}.start();
}
public int doWork(){
data[hasData++]=(int)(Math.random()*100);
try {
Thread.sleep(100);
}catch (InterruptedException e){
e.printStackTrace();
}
return hasData;
}
}
使用DatePickerDialog,TimePickerDialog
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/dateBn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="日期按钮"
/>
<Button
android:id="@+id/timeBn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="时间按钮"
/>
</LinearLayout>
package com.example.myapplication;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button dateBn = findViewById(R.id.dateBn);
Button timeBn = findViewById(R.id.timeBn);
dateBn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
EditText show = findViewById(R.id.show);
show.setText(year + " " + month + " " + dayOfMonth);
}
}, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();
}
});
timeBn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
EditText show = findViewById(R.id.show);
show.setText(hourOfDay + " " + minute);
}
}, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true).show();
}
});
}
}