【问题标题】:Toast with button in Android [duplicate]Android中带有按钮的吐司[重复]
【发布时间】:2014-06-04 06:42:01
【问题描述】:

如下图如何用按钮实现toast?它用于 Android 中最近的 Google 应用程序。

【问题讨论】:

标签: android android-button toast


【解决方案1】:

您可以像这样使用自定义吐司消息。这是一个将在 2.5 秒后关闭的对话框,就像敬酒一样。

public class CustomToast extends Dialog {  
 public CustomToast(Context context, String text) {  
  super(context);  
  requestWindowFeature(Window.FEATURE_NO_TITLE);  
  LayoutInflater inflater = (LayoutInflater) context  
  .getSystemService(android.content.Context.  
     LAYOUT_INFLATER_SERVICE);  
  
  View layout = inflater.inflate(R.layout.toast, null);  
  TextView tv = (TextView) layout.findViewById(R.id.toastText);  
  tv.setText(text);    
  setContentView(layout);  
  show();  
  setCanceledOnTouchOutside(true);  
  setCancelable(true);  
  Window window = getWindow();  
  window.setGravity(Gravity.BOTTOM);  
  new Handler().postDelayed(new Runnable() {  
  
   public void run() {  
    dismiss();  
   }  
  }, 2500);  
 }  
}  

【讨论】:

  • 尝试使用 Android 支持库中的 Snackbar
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-24
  • 1970-01-01
  • 2018-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多