触摸时取消 toast 消息
Cancel toast message on touch
我正在尝试让我的 toast 消息在触摸背景时过早消失。当触摸屏幕的每个部分时,toast 应该消失...我知道方法是 .cancel 但我不能正确使用它..这是我尝试过的:
我尝试使用 .setontouchlistener 方法,但它不起作用...我是 android 开发的新手,所以如果有示例代码可供学习...我将不胜感激我会告诉你我的代码,就是这样:
public class MainActivity extends AppCompatActivity {
private LayoutInflater layoutInflater;
private RelativeLayout relalayout
private LinearLayout linelayout;
private ScrollView scrollayout;
public Toast toast;
private ImageView backgroundimg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activitymain);
Bundle extradata1 = getIntent().getExtras();
String textString = extradata1.getString("ImportedData");
TextView mytext = (TextView) findViewById(R.id.text);
relalayout = (RelativeLayout) findViewById(R.id.relalayout);
linelayout = (LinearLayout) findViewById(R.id.linelayout);
scrollayout = (ScrollView) findViewById(R.id.scrolllayout);
backgroundimg = (ImageView) findViewById(R.id.imageView2);
toast = new Toast(this);
if (textString.equals("firstimported")) {
String mytxt = "";
StringBuffer sbuffer = new StringBuffer();
InputStream is = this.getResources().openRawResource(R.raw.firsttext);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
while ((mytxt = reader.readLine()) != null) {
sbuffer.append(mytxt + "\n");
}
mytext.setText(sbuffer);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
String text = sbuffer.toString();
SpannableString ss = new SpannableString(text);
ClickableSpan clickspan1 = new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast toast = Toast.makeText(MainActivity.this, "this is the toast",
Toast.LENGTH_SHORT);
toast.show();}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.WHITE);
ds.setUnderlineText(false);}
};
scrollayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
toast.cancel();
return false;
}
});
ss.setSpan(clickspan1, 52, 53, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mytext.setText(ss);
mytext.setMovementMethod(LinkMovementMethod.getInstance());
}
}
public void onBackPressed() {
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
finish();
}
当我触摸屏幕时 activity 崩溃了..所以它不能工作。
您必须在 activity 的变量
上声明 Toast
对象
private Toast toast;
然后在您的 onClick 方法上对其进行初始化:
toast = Toast.makeText(MainActivity.this, "this is the toast",Toast.LENGTH_SHORT);
toast.show();
此外,在您的 onTouch
上,您应该检查 toast 对象是否为空:
if(toast != null){
toast.cancel();
}
试试这个
Toast toast;
@Override
public void onClick(View widget) {
toast = Toast.makeText(MainActivity.this, "this is the toast", Toast.LENGTH_SHORT);
toast.show();
}
scrollayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(toast != null)
toast.cancel();
return super.onTouch(v, event);
}
});
根据代码更新答案
public class MainActivity extends AppCompatActivity {
private ScrollView scrollayout;
public Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activitymain);
TextView mytext = (TextView) findViewById(R.id.text);
scrollayout = (ScrollView) findViewById(R.id.scrolllayout);
StringBuffer sbuffer = new StringBuffer();
sbuffer.append("1234567890\n");
sbuffer.append("1234567890\n");
sbuffer.append("1234567890\n");
sbuffer.append("1234567890\n");
sbuffer.append("1234567890\n");
sbuffer.append("1234567890\n");
sbuffer.append("1234567890\n");
sbuffer.append("1234567890\n");
sbuffer.append("1234567890\n");
sbuffer.append("1234567890\n");
String text = sbuffer.toString();
SpannableString ss = new SpannableString(text);
ClickableSpan clickspan1 = new ClickableSpan() {
@Override
public void onClick(View widget) {
toast = Toast.makeText(MainActivity.this, "this is the toast", Toast.LENGTH_SHORT);
toast.show();
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.WHITE);
ds.setUnderlineText(false);
}
};
scrollayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (toast != null) {
toast.cancel();
toast = null;
}
return false;
}
});
ss.setSpan(clickspan1, 52, 53, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mytext.setText(ss);
mytext.setMovementMethod(LinkMovementMethod.getInstance());
}
}
我已经更改了您使用的一些值,因为我没有所有的资源,但是您需要做的主要更改是在 ClickSpan 初始化和 scrolllayout 触摸事件中。请比较它们。
显示吐司后,我通过滚动获得了以下输出。
长尺寸 gif 应用程序。
Toast.makeText returns Toast 对象。对此对象调用 cancel() 以取消它。
我正在尝试让我的 toast 消息在触摸背景时过早消失。当触摸屏幕的每个部分时,toast 应该消失...我知道方法是 .cancel 但我不能正确使用它..这是我尝试过的:
我尝试使用 .setontouchlistener 方法,但它不起作用...我是 android 开发的新手,所以如果有示例代码可供学习...我将不胜感激我会告诉你我的代码,就是这样:
public class MainActivity extends AppCompatActivity {
private LayoutInflater layoutInflater;
private RelativeLayout relalayout
private LinearLayout linelayout;
private ScrollView scrollayout;
public Toast toast;
private ImageView backgroundimg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activitymain);
Bundle extradata1 = getIntent().getExtras();
String textString = extradata1.getString("ImportedData");
TextView mytext = (TextView) findViewById(R.id.text);
relalayout = (RelativeLayout) findViewById(R.id.relalayout);
linelayout = (LinearLayout) findViewById(R.id.linelayout);
scrollayout = (ScrollView) findViewById(R.id.scrolllayout);
backgroundimg = (ImageView) findViewById(R.id.imageView2);
toast = new Toast(this);
if (textString.equals("firstimported")) {
String mytxt = "";
StringBuffer sbuffer = new StringBuffer();
InputStream is = this.getResources().openRawResource(R.raw.firsttext);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
while ((mytxt = reader.readLine()) != null) {
sbuffer.append(mytxt + "\n");
}
mytext.setText(sbuffer);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
String text = sbuffer.toString();
SpannableString ss = new SpannableString(text);
ClickableSpan clickspan1 = new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast toast = Toast.makeText(MainActivity.this, "this is the toast",
Toast.LENGTH_SHORT);
toast.show();}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.WHITE);
ds.setUnderlineText(false);}
};
scrollayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
toast.cancel();
return false;
}
});
ss.setSpan(clickspan1, 52, 53, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mytext.setText(ss);
mytext.setMovementMethod(LinkMovementMethod.getInstance());
}
}
public void onBackPressed() {
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
finish();
}
当我触摸屏幕时 activity 崩溃了..所以它不能工作。
您必须在 activity 的变量
上声明Toast
对象
private Toast toast;
然后在您的 onClick 方法上对其进行初始化:
toast = Toast.makeText(MainActivity.this, "this is the toast",Toast.LENGTH_SHORT);
toast.show();
此外,在您的 onTouch
上,您应该检查 toast 对象是否为空:
if(toast != null){
toast.cancel();
}
试试这个
Toast toast;
@Override
public void onClick(View widget) {
toast = Toast.makeText(MainActivity.this, "this is the toast", Toast.LENGTH_SHORT);
toast.show();
}
scrollayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(toast != null)
toast.cancel();
return super.onTouch(v, event);
}
});
根据代码更新答案
public class MainActivity extends AppCompatActivity {
private ScrollView scrollayout;
public Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activitymain);
TextView mytext = (TextView) findViewById(R.id.text);
scrollayout = (ScrollView) findViewById(R.id.scrolllayout);
StringBuffer sbuffer = new StringBuffer();
sbuffer.append("1234567890\n");
sbuffer.append("1234567890\n");
sbuffer.append("1234567890\n");
sbuffer.append("1234567890\n");
sbuffer.append("1234567890\n");
sbuffer.append("1234567890\n");
sbuffer.append("1234567890\n");
sbuffer.append("1234567890\n");
sbuffer.append("1234567890\n");
sbuffer.append("1234567890\n");
String text = sbuffer.toString();
SpannableString ss = new SpannableString(text);
ClickableSpan clickspan1 = new ClickableSpan() {
@Override
public void onClick(View widget) {
toast = Toast.makeText(MainActivity.this, "this is the toast", Toast.LENGTH_SHORT);
toast.show();
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.WHITE);
ds.setUnderlineText(false);
}
};
scrollayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (toast != null) {
toast.cancel();
toast = null;
}
return false;
}
});
ss.setSpan(clickspan1, 52, 53, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mytext.setText(ss);
mytext.setMovementMethod(LinkMovementMethod.getInstance());
}
}
我已经更改了您使用的一些值,因为我没有所有的资源,但是您需要做的主要更改是在 ClickSpan 初始化和 scrolllayout 触摸事件中。请比较它们。
显示吐司后,我通过滚动获得了以下输出。
长尺寸 gif 应用程序。
Toast.makeText returns Toast 对象。对此对象调用 cancel() 以取消它。