-1

我决定为 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 代码,它运行良好。例如:

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

2 回答 2

0

我终于找到了问题所在。感谢 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();
    }
});
}
}
于 2018-05-18T08:09:17.137 回答
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();
}
于 2018-05-18T06:04:48.283 回答