消息提示的三种方式

Posted sunshine_96

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了消息提示的三种方式相关的知识,希望对你有一定的参考价值。

\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'1.Toast

效果图:

 

 

(1)activity_mian.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.app2.MainActivity"
11     android:orientation="vertical">
12 
13     <Button
14         android:id="@+id/bt1"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:text="Button" />
18     <Button
19         android:id="@+id/bt2"
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:text="Button2" />
23 </LinearLayout>

2.MainAcivity.java

 1 package com.example.app2;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.view.Gravity;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.Toast;
 9 
10 public class MainActivity extends AppCompatActivity {
11     private Button button1,button2;
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_main);
16         button1=(Button)findViewById(R.id.bt1);
17         button2=(Button)findViewById(R.id.bt2);
18 
19         button1.setOnClickListener(new View.OnClickListener() {
20             @Override
21             public void onClick(View v) {
22                 Toast.makeText(MainActivity.this,"默认Toast..",Toast.LENGTH_SHORT).show();
23                 Toast.makeText(getApplicationContext(),R.string.toast1,Toast.LENGTH_SHORT).show();
24             }
25         });
26 
27         button2.setOnClickListener(new View.OnClickListener() {
28             @Override
29             public void onClick(View v) {
30                 Toast toast = Toast.makeText(MainActivity.this,"自定义Toast..",Toast.LENGTH_SHORT);
31                 //以中心位置为原点,向右移动200,向下移动200
32                 toast.setGravity(Gravity.CENTER,200,200);
33                 toast.show();
34             }
35         });
36     }
37 }

(3) 使用 

   的时候需要在

写入

1 <resources>
2     <string name="app_name">app2</string>
3     <string name="toast1">默认toast111...</string>
4 </resources>

 2.自定义图片的Toast。

(1)效果图:

(2)MianAcivity.java

 1  button3.setOnClickListener(new View.OnClickListener() {
 2             @Override
 3             public void onClick(View v) {
 4                 Toast toast = Toast.makeText(MainActivity.this,"自定义图片的Toast...",Toast.LENGTH_SHORT);
 5                 ImageView imageView = new ImageView(MainActivity.this);
 6                 LinearLayout linearLayout =(LinearLayout) toast.getView();
 7                 imageView.setImageResource(R.mipmap.ic_launcher);
 8                 linearLayout.addView(imageView);
 9                 toast.setGravity(Gravity.CENTER,0,0);
10                 toast.show();
11 
12             }
13         });

 3.完全自定义的Toast

(1)效果图:

 

 

 1  button4.setOnClickListener(new View.OnClickListener() {
 2             @Override
 3             public void onClick(View v) {
 4 
 5                 LayoutInflater layoutInflater = getLayoutInflater();
 6                 LinearLayout linearLayout =(LinearLayout) layoutInflater.inflate(R.layout.define,null);
 7                 TextView textView = (TextView) linearLayout.findViewById(R.id.tv1);
 8                 textView.setText("这是安全自定义的Toast");
 9 
10                 ImageView imageView = (ImageView)  linearLayout.findViewById(R.id.iv);
11                 imageView.setImageResource(R.mipmap.ic_launcher);
12 
13                 TextView textView1 = (TextView)  linearLayout.findViewById(R.id.tv2);
14                 textView1.setText("www.inspur");
15 
16                 Toast toast = new Toast(getApplicationContext());
17                 toast.setView(linearLayout);
18                 toast.setDuration(Toast.LENGTH_SHORT);
19                 toast.setGravity(Gravity.CENTER,0,0);
20                 toast.show();
21 
22 
23 
24             }
25         });

 

 

 

\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'2.AlertDialog

[1]:

(1):效果图

(2)MainActivity.java

 1 package com.example.app3;
 2 
 3 import android.content.DialogInterface;
 4 import android.support.v7.app.AlertDialog;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.Toast;
10 
11 public class MainActivity extends AppCompatActivity {
12     private Button button;
13 
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18         button = (Button) findViewById(R.id.bt);
19         button.setOnClickListener(new View.OnClickListener() {
20             @Override
21             public void onClick(View v) {
22                 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
23                 //1.设置标题
24                 builder.setTitle("登陆提示");
25                 //2.调用setIcon()显示图标
26                 builder.setIcon(R.mipmap.ic_launcher);
27                 //3.设置对话框内容
28                 builder.setMessage("是否登陆");
29 
30                 builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
31                     @Override
32                     public void onClick(DialogInterface dialog, int which) {
33                         Toast.makeText(getApplicationContext(),"您点击了确认按钮",Toast.LENGTH_SHORT).show();
34                     }
35                 });
36 
37                 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
38                     @Override
39                     public void onClick(DialogInterface dialog, int which) {
40                         Toast.makeText(getApplicationContext(),"您点击了取消按钮",Toast.LENGTH_SHORT).show();
41                     }
42                 });
43                 builder.create().show();
44             }
45         });
46     }
47 }

[2]:

(1)效果图: 选中之后点击确认,之后就可以显示

(2)MianActivity.java

 1  button2.setOnClickListener(new View.OnClickListener() {
 2             @Override
 3             public void onClick(View v) {
 4                 AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
 5                         .setTitle("简单列表")
 6                         .setIcon(R.mipmap.ic_launcher)
 7                         .setItems(str, new DialogInterface.OnClickListener() {
 8                             @Override
 9                             public void onClick(DialogInterface dialog, int which) {
10                                 info=str[which];
11                                 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
12                                 builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
13                                     @Override
14                                     public void onClick(DialogInterface dialog, int which) {
15                                        textView.setText(info);
16                                     }
17                                 });
18                                 builder.setNegativeButton("取消",null);
19                                 builder.create().show();
20                             }
21                         })
22 
23                         .create();
24                 alertDialog.show();
25 
26             }
27         });

 

[3]:

(1)效果图:点击多选按钮,点击确认,实现显示多选的选项

(2)MainActivity.java

声明的变量:

1   private Button button,button2,button3;
2     private  String[]  str={"java","xml","Android"};
3     private String info="";
4     private TextView textView;
5     private boolean[] isCheck ={false,false,false};
6     private List<String> list;

初始化:

1   button = (Button) findViewById(R.id.bt);
2         button2 = (Button) findViewById(R.id.bt2);
3         button3 = (Button) findViewById(R.id.bt3);
4         textView = (TextView) findViewById(R.id.tv);
5         list = new ArrayList<String>();

主要代码:

 1  button3.setOnClickListener(new View.OnClickListener() {
 2             @Override
 3             public void onClick(View v) {
 4                 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
 5                 //1.设置标题
 6                 builder.setTitle("多选提示");
 7                 //2.调用setIcon()显示图标
 8                 builder.setIcon(R.mipmap.ic_launcher);
 9                 //3.设置对话框的内容
10                 builder.setMultiChoiceItems(str, isCheck, new DialogInterface.OnMultiChoiceClickListener() {
11                     @Override
12                     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
13                         if (isChecked){
14                             list.add(str[which]);
15                         }else{
16                             list.remove(str[which]);
17                         }
18                     }
19                 });
20                 builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
21                     @Override
22                     public void onClick(DialogInterface dialog, int which) {
23                        textView.setText(list.toString());
24                     }
25                 });
26 
27                 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
28                     @Override
29                     public void onClick(DialogInterface dialog, int which) {
30                         textView.setText(list.toString());
31                     }
32                 });
33                 builder.create().show();
34             }
35         });
36 
37 
38     }




[5]单选效果,图片效果

 

 

 

 

 

----------------------------------总的MainActivity.java-------------------------------------

  1 package com.example.app3;
  2 
  3 import android.content.DialogInterface;
  4 import android.support.v7.app.AlertDialog;
  5 import android.support.v7.app.AppCompatActivity;
  6 import android.os.Bundle;
  7 import android.view.View;
  8 import android.widget.Button;
  9 import android.widget.ImageView;
 10 import android.widget.TextView;
 11 import android.widget.Toast;
 12 
 13 import java.util.ArrayList;
 14 import java.util.List;
 15 
 16 public class MainActivity extends AppCompatActivity {
 17     private Button button,button2,button3,button4,button5;
 18     private  String[]  str={"java","xml","Android"};
 19     private String info="";
 20     private TextView textView;
 21     private boolean[] isCheck ={false,false,false};
 22     private List<String> list;
 23 
 24     @Override
 25     protected void onCreate(Bundle savedInstanceState) {
 26         super.onCreate(savedInstanceState);
 27         setContentView(R.layout.activity_main);
 28         button = (Button) findViewById(R.id.bt);
 29         button2 = (Button) findViewById(R.id.bt2);
 30         button3 = (Button) findViewById(R.id.bt3);
 31         button4= (Button) findViewById(R.id.bt4);
 32         button5 = (Button) findViewById(R.id.bt5);
 33         textView = (TextView) findViewById(R.id.tv);
 34         list = new ArrayList<String>();
 35 
 36 
 37         button.setOnClickListener(new View.OnClickListener() {
 38             @Override
 39             public void onClick(View v) {
 40                 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
 41                 //1.设置标题
 42                 builder.setTitle("登陆提示");
 43                 //2.调用setIcon()显示图标
 44                 builder.setIcon(R.mipmap.ic_launcher);
 45                 //3.设置对话框内容
 46                 builder.setMessage("是否登陆");
 47 
 48                 builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
 49                     @Override
 50                     public void onClick(DialogInterface dialog, int which) {
 51                         Toast.makeText(getApplicationContext(),"您点击了确认按钮",Toast.LENGTH_SHORT).show();
 52                     }
 53                 });
 54 
 55                 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
 56                     @Override
 57                     public void onClick(DialogInterface dialog, int which) {
 58                         Toast.makeText(getApplicationContext(),"您点击了取消按钮",Toast.LENGTH_SHORT).show();
 59                     }
 60                 });
 61                 builder.create().show();
 62             }
 63         });
 64 
 65 
 66         button2.setOnClickListener(new View.OnClickListener() {
 67             @Override
 68             public void onClick(View v) {
 69                 AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
 70                         .setTitle("简单列表")
 71                         .setIcon(R.mipmap.ic_launcher)
 72                         .setItems(str, new DialogInterface.OnClickListener() {
 73                             @Override
 74                             public void onClick(DialogInterface dialog, int which) {
 75                                 info=str[which];
 76                                 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
 77                                 builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
 78                                     @Override
 79                                     public void onClick(DialogInterface dialog, int which) {
 80                                        textView.setText(info);
 81                                     }
 82                                 });
 83                                 builder.setNegativeButton("取消",null);
 84                                 builder.create().show();
 85                             }
 86                         })
 87 
 88                         .create();
 89                 alertDialog.show();
 90 
 91             }
 92         });
 93 
 94 
 95         button3.setOnClickListener(new View.OnClickListener() {
 96             @Override
 97             public void onClick(View v) {
 98                 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
 99                 //1.设置标题
100                 builder.setTitle("多选提示");
101                 //2.调用setIcon()显示图标
102                 builder.setIcon(R.mipmap.ic_launcher);
103                 //3.设置对话框的内容
104                 builder.setMultiChoiceItems(str, isCheck, new DialogInterface.OnMultiChoiceClickListener() {
105                     @Override
106                     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
107                         if (isChecked){
108                             list.add(str[which]);
109                         }else{
110                             list.remove(str[which]);
111                         }
112                     }
113                 });
114                 builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
115                     @Override
116                     public void onClick(DialogInterface dialog, int which) {
117                        textView.setText(list.toString());
118                     }
119                 });
120 
121                 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
122                     @Override
123                     public void onClick(DialogInterface dialog, int which) {
124                         textView.setText(list.toString());
125                     }
126                 });
127                 builder.create().show();
128             }
129         });
130 
131 
132         button4.setOnClickListener(new View.OnClickListener() {
133             @Override
134             public void onClick(View v) {
135                 ImageView imageView = new ImageView(MainActivity.this);
136                 imageView.setImageResource(R.mipmap.ic_launcher);
137                 AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
138                         .setTitle("简单列表")
139                         .setIcon(R.mipmap.ic_launcher)
140                         .setView(imageView)
141                         .setPositiveButton("确认",null)
142                         .setNegativeButton("取消",nullRocketMQ(02)——发送消息的三种方式

RocketMQ(02)——发送消息的三种方式

ActiveMQ持久化消息的三种方式

kafka传递消息的三种方式

Spring Cloud Stream实现消息过滤的三种主要方式

RabbitMQ笔记08消息队列RabbitMQ之防止消息丢失的三种方式(生产者消息确认消费者消息确认消息持久化)