您的位置:首页 > 其它

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

2020-03-02 03:31 113 查看
  • 在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 }

运行:

 

转载于:https://www.cnblogs.com/zulo/p/5093065.html

  • 点赞
  • 收藏
  • 分享
  • 文章举报
akai1223 发布了0 篇原创文章 · 获赞 0 · 访问量 264 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: