【问题标题】:Utility class for Android Toast objectAndroid Toast 对象的实用程序类
【发布时间】:2018-05-18 05:58:33
【问题描述】:

我决定为 Toast 编写自己的实用程序类来减少重复代码

public class Utilities {

public static void initializeToast(Context context, Toast toast, String res) {
    cancelToast(toast);
    toast = Toast.makeText(context, res, Toast.LENGTH_SHORT);
    toast.show();
}

public static void cancelToast(Toast toast){
    if (toast != null) {
        toast.cancel();
    }
}
}

如您所见,有两种方法。我想避免初始化堆叠的吐司,这就是我在新吐司之前取消旧吐司的方式。在我的客户端类中,我是这样使用它的:

public class AddGroupActivity extends AppCompatActivity {

private EditText mEditWordView;
private Toast toast;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_group);
    mEditWordView = findViewById(R.id.editText);
    final Button button = findViewById(R.id.add_group_button);
    button.setOnClickListener(view -> {
        if (TextUtils.isEmpty(mEditWordView.getText())) {
            Utilities.initializeToast(this, toast, "Message Example");
        }
    });
}
}

当我多次单击按钮时,我得到了堆积的 toast - 以前的 toast 没有被破坏。所以我需要帮助来定义它为什么会这样。

更新 早些时候,我在活动类中编写了 toast 代码,它运行良好。例如:

public class AddGroupActivity extends AppCompatActivity {

private EditText mEditWordView;
private Toast toast;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_group);
    mEditWordView = findViewById(R.id.editText);
    final Button button = findViewById(R.id.add_group_button);
    button.setOnClickListener(view -> {
        if (TextUtils.isEmpty(mEditWordView.getText())) {
            initToast("Message");
        }
    });
}

private void cancelToast() {
    if (toast != null) {
        toast.cancel();
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    cancelToast();
}

private void initToast(String res) {
    cancelToast();
    toast = Toast.makeText(this, res, Toast.LENGTH_SHORT);
    toast.show();
}
}

【问题讨论】:

  • 写一个条件来检查toast是否为空,然后尝试取消它。
  • toast.cancel();和 toast.hide();对清除所有堆叠的吐司没有用。
  • 你永远不会初始化你的private Toast toast;。它总是 null ,这就是为什么它不会取消任何东西
  • 为什么不呢? toast = Toast.makeText(context, res, Toast.LENGTH_SHORT);没有初始化?

标签: android toast utility-method


【解决方案1】:
public static void initializeToast(Context context, Toast toast, String res) {
    cancelToast(toast);
    if(toast == null){ // prevent create many instance of toast
       toast = Toast.makeText(context, res, Toast.LENGTH_SHORT);
    }else{
       toast.setText(res);
    }
    toast.show();
}

【讨论】:

  • 我检查了您的解决方案。它比我的和上一篇文章中的解决方案好一点,但仍然不理想。这个想法是,必须在新的 toast 出现后销毁之前的 toast。
【解决方案2】:

我终于找到了问题所在。感谢 Vladyslav Matviienko 的评论。我的私有字段被初始化为局部变量,在每次调用该方法后都会被销毁,这样 cancelToast(toast) 没有带来任何影响。

实用类

public class Utilities {

public static Toast initializeToast(Context context, Toast toast, String res) {
    cancelToast(toast);
    return Toast.makeText(context, res, Toast.LENGTH_SHORT);
}

public static void cancelToast(Toast toast) {
    if (toast != null) {
        toast.cancel();
    }
}
}

客户端类示例

public class AddGroupActivity extends AppCompatActivity {

private EditText mEditWordView;
private Toast toast;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_add_group);
  mEditWordView = findViewById(R.id.editText);
  final Button button = findViewById(R.id.add_group_button);
  button.setOnClickListener(view -> {
    if (TextUtils.isEmpty(mEditWordView.getText())) {
        toast = Utilities.initializeToast(this, toast, "Message Example");
        toast.show();
    }
});
}
}

【讨论】:

    猜你喜欢
    • 2017-11-02
    • 1970-01-01
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 2017-03-05
    相关资源
    最近更新 更多