无法显示自定义Toast

时间:2014-04-01 05:12:40

标签: android toast phone-state-listener

我有两个定制祝酒词。

当电话状态响起时,它将显示第一个吐司,当我点击该按钮时会有一个按钮,第一个吐司将被解雇,第二个吐司将被显示。

当我再次点击第二个吐司按钮时,它会关闭第二个吐司并显示第一个吐司。我怎样才能做到这一点?请尽快帮助我。

这是我的代码:

hideBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(numberToast!=null){
                numberToast.cancel();
                showTest_Toast();
            }

//------------------------------------------------------------------
public void showTest_Toast() {

    LayoutInflater inflater = LayoutInflater.from(ctx);

    View v = new View(ctx);

    test_layout = inflater.inflate(R.layout.test_toast,
            (ViewGroup) v.findViewById(R.id.test_id));

    testBtn = (Button) test_layout.findViewById(R.id.test_toast_button);

    testToast = new Toast(ctx);

    testToast.setGravity(Gravity.LEFT, 0, 0);

    testToast.setView(test_layout);

    // test_layout.setVisibility(View.INVISIBLE);

if (showButton == 1) {
        SharedPreferences timer_preferences = PreferenceManager
                .getDefaultSharedPreferences(ctx);

        String timer = timer_preferences.getString("duration", null);

        new CountDownTimer(Long.parseLong(timer), 1000) {
            public void onTick(long millisUntilFinished) {
                testToast.show();

            }

            public void onFinish() {

                // testToast.cancel();

            }
        }.start();
    } else {

        new CountDownTimer(50000, 1000) {
            public void onTick(long millisUntilFinished) {
                testToast.show();

            }

            public void onFinish() {

                // testToast.cancel();

            }
        }.start();
    }
    testBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            if (testToast!=null) {
                testToast.cancel();
                numberToast.show();

            } 

        }
    });
}

1 个答案:

答案 0 :(得分:0)

尝试打开自定义弹出屏幕。它很容易。

XML

<?xml version="1.0" encoding="utf-8"?>

<item
    android:id="@+id/item_movies"
    android:showAsAction="ifRoom|withText"
    android:title="Default Screen"
    android:visible="true"

    />
<item
    android:id="@+id/item_music"
    android:showAsAction="ifRoom|withText"
    android:title="Silent"
    android:visible="true"/>

java代码。

public void onWindowFocusChanged(boolean hasFocus) {

    int[] location = new int[2];
    Button button = (Button) myview.findViewById(R.id.slidehandlebutton);

    // Get the x, y location and store it in the location[] array

    button.getLocationOnScreen(location);

    // Initialize the Point with x, and y positions
    p = new Point();
    p.x = location[0];
    p.y = location[1];
}

private void showPopup(Unlock_hud unlock_hud, Point p2) {
    // TODO Auto-generated method stub
    int popupWidth = 180;
    int popupHeight = 80;


    LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layout1 = layoutInflater.inflate(R.layout.popup_layout, null);


    // Creating the PopupWindow

    // Some offset to align the popup a bit to the right, and a bit down,
    // relative to button's position.
    int OFFSET_X = -200;
    int OFFSET_Y = 5;


    // Displaying the popup at the specified location, + offsets.
    params2.x =20;
    params2.y = 40;
    wm2.addView(layout1, params2);
    isDialogShow = true;

    // Getting a reference to Close button, and close the popup when
    // clicked.
    Button close = (Button) layout1.findViewById(R.id.stop);
    close.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            wm2.removeView(layout1);
            isDialogShow = false;
            onDestroy();
        }
    });
点击按钮

case R.id.slidehandlebutton:
        if (isDialogShow) {
            DissmissPopup();
        } else {
            showPopup(Unlock_hud.this, p);
        }