高级组件之消息提示框与对话框

1.消息提示框
(1)使用Toast显示消息提示框
setduration(int duration) 设置消息提示框持续时间
setgravity(int gravity,int xoffset,int yoffset) 设置消息提示框的位置
setmargin(float horizontalMargin,float verticalMargin) 设置消息提示的页边距
settext(CharSequence s) 设置要显示的文本内容
setview(View view) 设置将要在消息提示框中显示的视图
有两种方法创建消息提示框
第一种通过make Text()方法创建消息提示框
Toast.makeText(this, "11111111", Toast.LENGTH_SHORT).show();
第二种通过构造方法创建消息提示框
Toast toast = new Toast(this);
LinearLayout linearLayout1 = new LinearLayout(this);
TextView tv = new TextView(this);
tv.setText("222222222222");
linearLayout1.addView(tv);
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.ic_launcher);
linearLayout1.addView(view);
toast.setView(linearLayout1);
toast.setDuration(toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
(2)使用notification在状态栏上显示通知,大致可以分为以下4个步骤
①调用etSystemService()方法获取系统的NotificationManager服务
②创建一个Notification对象,并为其设置各种属性
③为Notification对象设置事件信息
④通过NotificationManager类的notify()方法发送Notification通知
final NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Notification notify = new Notification();
notify.icon=R.drawable.ic_launcher;
notify.tickerText="显示第一个通知";
notify.when=System.currentTimeMillis();
notify.defaults=Notification.DEFAULT_ALL;
notify.setLatestEventInfo(NotificationActivity.this, "无题", "每天进步一点点", null);
notificationManager.notify(NOTIFYID_1, notify);
Notification notify1 = new Notification(R.drawable.img2,"显示第二个通知",System.currentTimeMillis());
notify1.flags|=Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(NotificationActivity.this,ContentActivity.class );
PendingIntent pendingIntent = PendingIntent.getActivity
(NotificationActivity.this, 0, intent, 0);
notify.setLatestEventInfo(NotificationActivity.this, "通知", "查看详细内容", pendingIntent);
notificationManager.notify(NOTIFYID_2, notify);
}
});
Button button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
notificationManager.cancelAll();
}
});
2.使用AlertDialog创建对话框
AlertDialog类常用的方法
settitle() 为对话框设置标题
seticon() 为对话框设置图标
setmessage() 为对话框设置要显示的内容
setbutton() 为对话框添加按钮
AlertDialog.Builder类常用的方法,AlertDialog.Builder拥有AlertDialog的所有方法
setNegativeButton() 为对话框添加取消按钮
setPositiveButton() 为对话框添加确定按钮
setNeutralButton() 为对话框添加中立按钮
setItems() 为对话框添加列表项
setSingleChoiceItems() 为对话框添加单选列表项
setMultiChoiceItems() 为对话框添加多选列表项
(1)在布局文件中添加四个用于控制各种对话框显示的按钮:
<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4" />
(2)为按钮1添加监听事件,应用AlertDialog类创建一个带有取消、中立和确定按钮的提示对话框
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setIcon(R.drawable.img21);
alert.setTitle("系统提示:");
alert.setMessage("带取消、中立和确定按钮的对话框!");
alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你单击了取消按钮", Toast.LENGTH_SHORT).show();
}
});
                alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你单击了确定按钮", Toast.LENGTH_SHORT).show();
}
});
                alert.setButton(DialogInterface.BUTTON_NEUTRAL, "中立", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {}
                });
                alert.show();
}
});

高级组件之消息提示框与对话框_第1张图片

高级组件之消息提示框与对话框_第2张图片

(3)为按钮2添加监听事件,应用AlertDialog.Builder类创建一个带5个列表项的列表对话框
Button button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
final String[] items = new String[]{"跑步","羽毛球","乒乓球","网球","体操"};
Builder builder = new Builder(MainActivity.this);
builder.setIcon(R.drawable.img22);
builder.setTitle("请选择你喜欢的项目");
builder.setItems(items, new OnClickListener() {

@Override
public void onClick(DialogInterface dialogInterface, int which) {
Toast.makeText(MainActivity.this, "你选择了"+items[which], Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
}
});

高级组件之消息提示框与对话框_第3张图片

高级组件之消息提示框与对话框_第4张图片

(4)为按钮3添加监听事件,应用AlertDialog.Builder类创建一个带5个单选列表项和一个确定按钮的列表对话框
Button button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
final String[] items = new String[]{"标准","无声","会议","户外","离线"};
Builder builder = new Builder(MainActivity.this);
builder.setIcon(R.drawable.img23);
builder.setTitle("请选择要使用的情景模式");
builder.setSingleChoiceItems(items, 0,new OnClickListener() {

@Override
public void onClick(DialogInterface dialogInterface, int which) {
Toast.makeText(MainActivity.this, "你选择了"+items[which], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", null);
builder.create().show();
}
});

高级组件之消息提示框与对话框_第5张图片

(5)为按钮4添加监听事件,应用AlertDialog.Builder类创建一个带5个多选列表项和一个确定按钮的列表对话框
Button button4 = (Button)findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
checkedItems=new boolean[]{false,true,false,true,false};
items=new String[]{"植物大战僵尸","愤怒的小鸟","泡泡龙","开心农场","超级玛丽"};
Builder builder = new Builder(MainActivity.this);
builder.setIcon(R.drawable.img24);
builder.setTitle("请选择你喜爱的游戏");
builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {

@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems[which]=isChecked;
}
});
builder.setPositiveButton("确定", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
String result="";
for (int i = 0; i < checkedItems.length; i++) {
if (checkedItems[i]) {
result+=items[i]+"、";
}
}
if (!"".equals(result)) {
result=result.substring(0,result.length()-1);
Toast.makeText(MainActivity.this, "你选择了["+result+"]", Toast.LENGTH_SHORT).show();
}
}
});
builder.create().show();
}
});
}

高级组件之消息提示框与对话框_第6张图片

高级组件之消息提示框与对话框_第7张图片

你可能感兴趣的:(高级组件之消息提示框与对话框)