【发布时间】:2011-12-09 20:03:33
【问题描述】:
我有按钮。当用户点击按钮时,有一些条件,条件不满足则需要显示 Toast 但不显示 Toast 消息...
代码:已编辑
Button addMe = (Button)findViewById(R.id.addMe);
addMe.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(selectedReason.equals("--Select--")){
Log.i("TAG","-----");
Toast.makeText(getBaseContext(), "Reason can not be blank", Toast.LENGTH_SHORT).show();
}else if(selectedType.equals("--Select--")){
Toast.makeText(getParent(), "Discount type can not be blank", Toast.LENGTH_SHORT).show();
}else{
if(selectedType.equals("Value")){
if(spc_amount.getText().toString().equals("")){
Log.i("TAG","-----");
Toast.makeText(getBaseContext(), "Discount type can not be blank", Toast.LENGTH_SHORT).show();
}else{
if(Double.parseDouble(spc_amount.getText().toString()) > invoiceValue){
Toast.makeText(getBaseContext(), "Amonut can not be grater than invoice", Toast.LENGTH_SHORT).show();
}else{
Discount dis = new Discount();
dis.setCriteriaName(selectedReason);
dis.setDiscountValue(Double.parseDouble(spc_amount.getText().toString()));
spDisList.put(1,dis);
tl.removeAllViews();
loadTableLayout();
}
}
}
}
}
});
我尝试过使用 getParent() 、 getApplicationContext() 、 SpecialDiscountActivity.this 和 getBaseContext() 的上下文,但没有工作......
这条Toast消息来自Tab Activity Group
【问题讨论】:
-
正如下面的一条评论所指出的,您还应该为应用程序启用“显示通知”,以便 Toast 为您工作。
-
为什么要编辑代码以包含解决方案?应该保留它以包含错误,否则这个问题没有意义。
-
还有一个常见的陷阱:如果你不在主线程中,你需要调用
runOnUiThread。例如。runOnUiThread(() -> Toast.makeText(getApplicationContext(), " ...", Toast.LENGTH_LONG).show());
标签: android