Button in custom Android Toast?

前端 未结 8 1065
天命终不由人
天命终不由人 2020-11-30 06:01

Is it possible to have a button in a Toast?

In theory, yes because you can build a custom Toast from a layout in XML, but I tried to put a button in it and couldn\

相关标签:
8条回答
  • 2020-11-30 06:07

    You can try SuperToast in this case. It can create toast with button. It has custom duration feature, colourful background, colourful fonts, custom fonts, animated effect. Hope u will enjoy it

    0 讨论(0)
  • 2020-11-30 06:09

    You should use a Snackbar. It is in the latest android support library(at time of answer) and is compatible with older api levels. It is much easier to implement than a Dialog or custom View and has the ability to have a button unlike a Toast.

    1. Download Android Support Library from Extras in the SDK Manager(revision 22.2.1 or later).
    2. In the build.gradle add this to the class dependencies: com.android.support:design:22.2.0.
    3. Implement:

      Snackbar.make(this.findViewById(android.R.id.content), "Toast Message", Snackbar.LENGTH_LONG) .setAction("Click here to activate action", onClickListener) .setActionTextColor(Color.RED) .show;

    And that is it. No github projects and implementation is very similiar to Toast. I used it in one of my projects and it works great.

    0 讨论(0)
  • 2020-11-30 06:11

    Creating a system overlay window (always on top)

    This is suggesting that it can be done, I also need buttons in a toast so I still have to make my own implementation. If I find more I will add it to my post

    0 讨论(0)
  • 2020-11-30 06:17

    Use an alertbox, if you want to add a button :-). Here are some examples Dialog boxes in Android

    0 讨论(0)
  • 2020-11-30 06:22

    A custom view passed to a toast can contain anything; however, toasts cannot receive any touch events so no components that use touch events will work in a stock toast (buttons, radiobuttons, etc.). The only choice you have is to create a custom view with a button in it and add it to your layout. There are many examples of how to do this and a few libraries you can check out to see how other people are doing it.

    UndoBar
    MessageBar
    Nurik's UndoBar

    Of course you are also welcome to use the SuperToasts library I put together however it might be a little overkill for one usage. The way that I do it is outlined in the SuperActivityToast class.

    0 讨论(0)
  • 2020-11-30 06:23

    A toast can not be clicked. It is not possible to capture a click inside a toast message. You will need to build a dialog for that. Look at Creating Dialogs for more info.

    The API on the Toast class state that a toast will never receive the focus and because a toast is not a view there is no onClick message. I would assume that therefore childs of a Toast can not be clicked as well.

    0 讨论(0)
提交回复
热议问题