码迷,mamicode.com
首页 > 其他好文 > 详细

提示框的优化之自定义Toast组件之(二)Toast组件的业务逻辑实现

时间:2016-01-01 11:12:52      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

  • 在java下org.socrates.mydiary.activity下LoginActivity下自定义一个方法showCustomerToast() ?
 1 public class LoginActivity extends AppCompatActivity {
 2      private void showCustomerToast(final int icon, final String message){
 3          LayoutInflater inflater=getLayoutInflater();    //通过获取LayoutInflater对象创建一个LayoutInflater接口对象
 4          View layout=inflater.inflate(R.layout.toast_customer, (ViewGroup) findViewById(R.id.toast_layout_root));  //使用Inflater对象中Inflater方法绑定自定义Toast的布局文件,同时指向该布局文件中跟标记节点
 5  
 6          ImageView toastIcon=(ImageView)layout.findViewById(R.id.toastIcon);
 7          toastIcon.setBackgroundResource(icon);
 8  
 9          TextView toastMessage = (TextView)layout.findViewById(R.id.toastMessage); //获取该布局文件中的TextView组件并为其动态赋值
10          toastMessage.setText(message);
11  
12          Toast toast=new Toast(getApplicationContext());  //实例化一个Toast组件对象
13          toast.setDuration(Toast.LENGTH_LONG);  
14          toast.setView(layout);  ////将设置好的定制布局与当前的Toast对象进行绑定
15          toast.show();  //显示Toast组件
16      }
17 }
18  

业务逻辑流程:

(1)通过获取LayoutInflater对象创建一个LayoutInflater接口对象

(2)使用Inflater对象中Inflater方法绑定自定义Toast的布局文件,同时指向该布局文件中跟标记节点

(3)获取该布局文件中的TextView组件并为其动态赋值

(4)实例化一个Toast组件对象

(5)将设置好的定制布局与当前的Toast对象进行绑定

(6)显示Toast组件

  • 在指定位置调用该方法
 1 private  class ViewOcl implements View.OnClickListener{
 2         @Override
 3         public void onClick (View v){
 4             switch (v.getId()){
 5                 case R.id.btnLogin:
 6                     String account=txtAccount.getText().toString().trim();
 7                     String password=txtPassword.getText().toString().trim();
 8                     boolean login_flag =false;
 9                             
10                     if (login_flag) {
11                         showCustomerToast(android.R.drawable.ic_menu_call,"欢迎登录," + account);  //在指定位置调用该方法
12                
13                         break;
14               
15                     }
16                     else {
17                         showCustomerToast(android.R.drawable.ic_delete,"账号或密码错误");  //在指定位置调用该方法
18                     }
19                     break;
20              }
21         }
22 }

运行:

技术分享

 

提示框的优化之自定义Toast组件之(二)Toast组件的业务逻辑实现

标签:

原文地址:http://www.cnblogs.com/zulo/p/5093065.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!