Android- Making Toast clickable

∥☆過路亽.° 提交于 2020-01-16 04:11:07

问题


I want to make toast click able or a UI element which will have clickable button but behaves like toast. It should not run on UI thread like toast. It should not halt or overlay current user activity, message should come like toast with clickable button and vanish but as toast user should be able to access background ongoing UI item.

If any one has any idea about how to achieve this pls share with me.


回答1:


The Gmail undo bar is the best suitable for you, its just like a toast with a button. Here is a code implementation for it.

http://code.google.com/p/romannurik-code/source/browse/misc/undobar/src/com/example/android/undobar/UndoBarController.java




回答2:


I had a similar requirement that I solved using a PopupWindow. Basically, I had an about window with a clickable link that I wanted displayed like a toast. The popup window can accomplish this as follows:

In the parent class, I use the following flag:

private boolean durationExpired = false;

Then, when I invoke what would have been the toast, I do the following instead:

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.about_hiittimer,
    (ViewGroup) findViewById(R.id.about_hiittimer));
TextView url = (TextView) layout.findViewById(R.id.url);
url.setMovementMethod(LinkMovementMethod.getInstance());

final PopupWindow popupWindow = new PopupWindow(layout, 280, 160, false);
popupWindow.showAtLocation(layout, 17, 0, 0);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);

final Handler popupHandler = new Handler();
runOnUiThread(new Runnable() {
    @Override
    public void run() {
        if (!durationExpired) {
            durationExpired = true;
            popupHandler.postDelayed(this, 2000);
        } else {
            popupWindow.dismiss();
            popupHandler.removeCallbacks(this);
            durationExpired = false;
        }
    }
});



回答3:


Little trick. Tested working Android 4.4

toast = new Toast(context);
try {
    Class<?> clazz = Class.forName("android.widget.Toast");
    Method method = clazz.getDeclaredMethod("getWindowParams");
    WindowManager.LayoutParams param = (WindowManager.LayoutParams) method.invoke(toast);
    param.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
} catch (Exception e) {
    e.printStackTrace();
}



回答4:


Put your main layout inside a FrameLayout. Write a layout for your toast, with the button and all, insert it into the FrameLayout (below your main layout) and set its visibility to GONE.

When you show it (setting visibility to VISIBLE) start a new thread that counts down the seconds till its dismissed. Set it back to invisible from the thread via a Handler (cause all UI elements can only be managed from the main thread).

cheers!



来源:https://stackoverflow.com/questions/5919348/android-making-toast-clickable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!