Android Toast对象的实用程序类

时间:2018-05-18 05:58:34

标签: android toast utility-method

我决定为Toast编写自己的Utility类来剪切重复代码

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-code,它运行得很好。例如:

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();
}
}

2 个答案:

答案 0 :(得分:0)

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();
}

答案 1 :(得分:0)

我终于找到了问题所在。感谢Vladyslav Matviienko的评论。我的私有字段被初始化为局部变量,在每次调用方法后都被销毁,并且取消Toast(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();
    }
});
}
}