好记性不如烂笔杆android学习笔记<十二> Toast的使用

24,//Toast的使用
<1>//Toast,界面上显示一个提示,长时间和短时间显示 ,在toast.xml文件中2个button

 1 public class SubToastActivity extends Activity{
 2     private Button shortButton = null;
 3     private Button longButton = null;
 4     private static int NOTIFICATION_ID = R.layout.activity_toast;
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_toast);
 9         
10         shortButton = (Button)findViewById(R.id.short_time_but);
11         shortButton.setOnClickListener(new OnClickListener() {
12             
13             @Override
14             public void onClick(View v) {
15                 setTitle("短时间显示Toast");
16                 showToast(Toast.LENGTH_SHORT);
17             }
18         });
19         longButton = (Button)findViewById(R.id.long_time_but);
20         longButton.setOnClickListener(new OnClickListener() {
21             
22             @Override
23             public void onClick(View v) {
24                 setTitle("长时间显示Toast");
25                 showToast(Toast.LENGTH_LONG);
26             }
27         });
28     }
29     protected void showToast(int type) {
30         View view = inflateView(R.layout.toast);
31         TextView tv = (TextView) view.findViewById(R.id.content);
32         tv.setText("Android应用开发Android应用开发Android应用开发Android应用开发");
33         Toast toast = new Toast(this);
34         toast.setView(view);
35         toast.setDuration(type);
36         toast.show();
37     }
38     private View inflateView(int resource){
39         LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
40         return vi.inflate(resource, null);
41     }

<2>可以输出Image的Toast

 1 public class ShowImageToast extends Activity{
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.showimage);
 7         Toast showImageToast = new Toast(this);
 8         ImageView imageView = new ImageView(this);
 9         imageView.setImageResource(R.drawable.default_icon);
10         showImageToast.setView(imageView);
11         showImageToast.setDuration(Toast.LENGTH_LONG);
12         showImageToast.show();
13     }
14 }

 

<3>输出字符可以是字符串连接
Toast.makeText(DearToast.this, str2+Str.toString()+str3,Toast.LENGTH_LONG).show();

原文地址:https://www.cnblogs.com/zjqlogs/p/2780231.html