Android Toast就像弹出一样

时间:2014-03-24 02:38:29

标签: android message toast

我一直在使用Toast但是android有类似的东西,但是消息会一直显示,直到用户自己关闭它为止?

3 个答案:

答案 0 :(得分:1)

您可以使用Dialog 阅读此页面。 http://developer.android.com/reference/android/app/Dialog.html

答案 1 :(得分:1)

你可以试试Android Crouton:

Android Croutons

Github Link

Crouton是一个可供Android开发人员使用的类,他们觉得需要替代Context不敏感的Toast。

Crouton将显示在开发人员决定的位置。标准将是应用程序窗口的顶部。您可以排列多个Croutons进行显示,这些将一个接一个地显示。

答案 2 :(得分:1)

据我所知,你有三个选择来完成这项任务。

  1. Custome Toast
  2. PopupWindow
  3. 小吃吧

    CostumeToast

      void errortoast(String Msg) {
    
    LayoutInflater inflater = getLayoutInflater();
    
    View layout = inflater.inflate(R.layout.toast,
            (ViewGroup) findViewById(R.id.custom_toast_layout));
    
    TextView text = (TextView) layout.findViewById(R.id.textToShow);
    
    text.setText(Msg);
    
    Toast toast = new Toast(getApplicationContext());
    
    toast.setGravity(Gravity.TOP | 0, 0, 0);
    
    toast.setDuration(Toast.LENGTH_SHORT);
    
    toast.setView(layout);
    
    toast.show();
    
    }
    
  4. <强> toast.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_layout"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/background_dark"
    android:orientation="horizontal"
    android:padding="8dp" >
    
    <TextView
        android:id="@+id/textToShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@android:drawable/ic_dialog_alert"
        android:text="Error"
        android:textColor="#FFFFFF" 
        android:textSize="19sp"/>
    
    </LinearLayout>
    

    <强> PopUpWindow

        public void displaypopup() {
    
        LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                .getSystemService(LAYOUT_INFLATER_SERVICE);
        View popupView = layoutInflater.inflate(R.layout.popup, null);
        final PopupWindow popupWindow = new PopupWindow(popupView,
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    
        Button btnDismiss = (Button) popupView.findViewById(R.id.dismiss);
        btnDismiss.setOnClickListener(new Button.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        popupWindow.showAtLocation(popupView, Gravity.TOP, 0, 0);
    }
    

    <强> popup.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lytpopup"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >
    
    <TextView
        android:id="@+id/tvmessagedialogtitle"
        android:layout_width="wrap_content"
    
        android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" 
        android:layout_gravity="center_horizontal"
        android:text="Hello" />
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal" >
    
        <Button
            android:id="@+id/bmessageDialogYes"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:background="#cb4c4c"
            android:minHeight="0dp"
            android:minWidth="0dp"
            android:paddingBottom="5dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="5dp"
            android:text="View"
            android:textColor="#FFFFFF"
            android:textSize="18dip" />
    
        <Button
            android:id="@+id/dismiss"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#77aa5c"
            android:minHeight="0dp"
            android:minWidth="0dp"
            android:paddingBottom="5dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="5dp"
            android:text="Dismiss"
            android:textColor="#FFFFFF"
            android:textSize="18dip"
            android:textStyle="normal"
            android:typeface="normal" />
    </LinearLayout>
    

    了解更多信息read

    1. 小吃吧
    2. for snackbar尝试this教程