zoukankan      html  css  js  c++  java
  • 提示框的优化之自定义Toast组件之(二)Toast组件的业务逻辑实现

    • 在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 }

    运行:

    花朵开放的时候花蕾消逝,人们会说花蕾是花朵否定了的;同样地,当结果的时刻花朵又被解释为植物的一种虚假的存在形式,而果实是作为植物的真实形式而取代花朵的。这些形式不但彼此不同,而且互相排斥,互不相容。但是,他们的流动性却使他们成为有机统一体的环节,他们在有机统一体中不但互相抵触,而且彼此都同样是必要的;而正是这种同样的必要性才构成整体的生命。
  • 相关阅读:
    hdu 2842 Chinese Rings
    Codeforces Round #118 (Div. 1) A 矩阵快速幂
    hdu2604 Queuing
    支付宝 生活号 获取 userId 和 生活号支付
    maven 项目使用本地jar
    nexus 私有 maven 仓库的搭建
    linux jdk 安装
    gitlab 可以上传代码,但是 不能 上传 tag 问题
    maven 内置变量
    mysql 不允许分组的问题 this is incompatible with sql_mode=only_full_group_by
  • 原文地址:https://www.cnblogs.com/zulo/p/5093065.html
Copyright © 2011-2022 走看看