Android 如何在演示课上显示Toast消息和对话框?
我为辅助监视器创建了演示类。我想在第二个监视器上显示一些对话框 对于toast,我发现: 但它不起作用 我唯一能想到的就是在我的基本布局上建立一个新的视图。 但是,它不能像对话框那样轻松地控制视图 这是我的托斯塔达。我将其更改为使用Android 如何在演示课上显示Toast消息和对话框?,android,dialog,android-toast,presentation,Android,Dialog,Android Toast,Presentation,我为辅助监视器创建了演示类。我想在第二个监视器上显示一些对话框 对于toast,我发现: 但它不起作用 我唯一能想到的就是在我的基本布局上建立一个新的视图。 但是,它不能像对话框那样轻松地控制视图 这是我的托斯塔达。我将其更改为使用Tostada.makeText(上下文、文本、时间) object Tostada { private var mView: TextView? = null private var mWM: WindowManager? = null pr
Tostada.makeText(上下文、文本、时间)
object Tostada {
private var mView: TextView? = null
private var mWM: WindowManager? = null
private val mParams = WindowManager.LayoutParams()
private var mContext: Context? = null
private val mHideRunnable = Runnable { handleHide() }
/**
* Assign the Tostada context. It can be a Presentation context. Remember to call setContext(null)
* to clear the stored context when you no longer need this class, otherwise memory leaks can occur.
*
* @param context The context where the Tostada will be rendered
*/
/**
* Creates a Tostada with a gravity ana an offset x, y
*
* @param offsetX The X offset in pixels to apply to the gravity's location.
* @param offsetY The Y offset in pixels to apply to the gravity's location.
* @paran gravity The gravity constant
*/
fun makeText(context: Context, text: String?, timeout: Int) {
mContext = context
mParams.packageName = context.packageName
mView = TextView(context)
mWM = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val params = mParams
params.height = WindowManager.LayoutParams.WRAP_CONTENT
params.width = WindowManager.LayoutParams.WRAP_CONTENT
params.format = PixelFormat.TRANSLUCENT
params.windowAnimations = 0
params.type = WindowManager.LayoutParams.TYPE_TOAST
params.title = "Toast"
params.flags = (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
or WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
mParams.gravity = Gravity.BOTTOM or Gravity.CENTER_VERTICAL
mParams.x = 0
mParams.y = 50
mParams.verticalMargin = 0f
mParams.horizontalMargin = 0f
checkNotNull(mContext) { "You must assign a context to this Tostada before it vcan be used" }
// remove old timer for an ongoing tostada, if any
mView!!.removeCallbacks(mHideRunnable)
mView!!.text = text
// Add the view to the window manager, if not already added
if (mView!!.parent == null) mWM!!.addView(mView, mParams)
// schedule for removal
if (timeout > 0) mView!!.postDelayed(mHideRunnable, timeout.toLong())
}
private fun handleHide() {
// remove the view if added
if (mView!!.parent != null) mWM!!.removeView(mView)
}
}