Android 删除多个吐司?
我在布局上设置了一个按钮,若用户单击该按钮,将显示toastAndroid 删除多个吐司?,android,toast,Android,Toast,我在布局上设置了一个按钮,若用户单击该按钮,将显示toast button.setOnClickListener(toastListener); OnClickListener toastListener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast error = Toast
button.setOnClickListener(toastListener);
OnClickListener toastListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast error = Toast.makeText(this, msg, Toast.LENGTH_LONG);
error.show();
}
};
但当用户多次单击按钮时,将显示更多的toast。无论用户点击按钮多少次,我都可以在屏幕上显示一个祝酒词吗?
非常感谢我使用以下方法来实现这一点
private void showToastMessage(final String message) {
mHandler.post(new Runnable() {
public void run() {
if (mToast == null) {
if (getActivity() != null) {
mToast = Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT);
}
}
if (getActivity() != null) {
mToast.setText(message);
mToast.show();
}
}
});
}
我使用以下方法来实现这一点
private void showToastMessage(final String message) {
mHandler.post(new Runnable() {
public void run() {
if (mToast == null) {
if (getActivity() != null) {
mToast = Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT);
}
}
if (getActivity() != null) {
mToast.setText(message);
mToast.show();
}
}
});
}
我还没有真正尝试过,但我怀疑只要在下一次点击时取消它并制作一个新的就可以了
Toast mToast;
public void onContentChanged() {
...
button.setOnClickListener(toastListener);
OnClickListener toastListener = new OnClickListener() {
@Override
public void onClick(View v) {
if(mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
mToast.show();
}
};
我还没有真正尝试过,但我怀疑只要在下一次点击时取消它并制作一个新的就可以了
Toast mToast;
public void onContentChanged() {
...
button.setOnClickListener(toastListener);
OnClickListener toastListener = new OnClickListener() {
@Override
public void onClick(View v) {
if(mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
mToast.show();
}
};