Android 实验四
一.实验目的
对话框、Toast的使用
二.使用的设备和仪器
计算机+Windows 10+Android studio
三.实验内容及要求
对话框、Toast的使用
四.实验步骤
1.程序代码:
//简单对话框
Button btnDialog1 = (Button) findViewById(R.id.btnDialog1);
btnDialog1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
builder.setTitle("简单对话框");
builder.setIcon(R.drawable.ic_launcher_foreground);
builder.setMessage("这是提示文字");
builder.setPositiveButton("确定", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {                        Toast.makeText(MainActivity.this, "确定",
Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("取消", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {                        Toast.makeText(MainActivity.this, "取消",
Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = ate();
dialog.show();
}
});
//列表型对话框
Button btnDialog2 = (Button) findViewById(R.id.btnDialog2);
btnDialog2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
builder.setTitle("列表型对话框");
builder.setIcon(R.drawable.ic_launcher_foreground);
final String[] str = new String[]{"aaa","bbb","ccc"};
builder.setItems(str, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,str[which],Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = ate();
dialog.show();
}
});
//单选型
Button btnDialog3 = (Button) findViewById(R.id.btnDialog3);
btnDialog3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
builder.setTitle("单选型对话框");
builder.setIcon(R.drawable.ic_launcher_foreground);
final String[] str = new String[]{"aaa","bbb","ccc"};
builder.setSingleChoiceItems(str, 1, new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {                    }
});
builder.setPositiveButton("确定", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,str[which],Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("取消", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "取消",
Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = ate();
dialog.show();
}
alertdialog使用方法
});
//自定义
Button btnDialog4 = (Button) findViewById(R.id.btnDialog4);
btnDialog4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
builder.setTitle("自定义对话框");
builder.setIcon(R.drawable.ic_launcher_foreground);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View view = inflater.inflate(ast_layout,null);
builder.setView(view);
builder.setPositiveButton("确定", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "确定",
Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("取消", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "取消",
Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = ate();
dialog.show();
}
2.运行结果:
五.实验总结
通过这次实验,学会了Toast的使用;掌握了几种常见的对话框,简单对话框、列表型对话框、单选型对话框、自定义对话框。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。