我需要显示一个最小侵入性的非阻塞通知,它与它显示的活动无关(如 a Toast
)并且是可点击的。有人知道这是否可能吗?不幸的是,Toast
通知(自定义或其他)似乎不可点击(即OnClickListener
在其视图上设置一个无效)。而且,如果我错了,请纠正我,所有替代项 ( AlertDialg
, PopupWindow
, Crouton
) 都会显示一个与它显示的活动相关的通知(即,当活动完成时它们不会继续显示)。有什么建议么?
4 回答
您可以使用PopupWindow
、添加onClickListener
和添加 ahandler
以在 n 次后自动取消它(就像 a 的行为toast
)。像这样的东西:
public static void showToast(Activity a, String title, String message) {
// inflate your xml layout
LayoutInflater inflater = a.getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) a.findViewById(R.id.toast_layout_root));
// set the custom display
((TextView) layout.findViewById(R.id.title)).setText(title);
((TextView) layout.findViewById(R.id.message)).setText(message);
// initialize your popupWindow and use your custom layout as the view
final PopupWindow pw = new PopupWindow(layout,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, true);
// set windowType to TYPE_TOAST (requires API 23 above)
// this will make popupWindow still appear even the activity was closed
pw.setWindowLayoutType(WindowManager.LayoutParams.TYPE_TOAST);
pw.showAtLocation(layout, Gravity.CENTER | Gravity.TOP, 0, 500);
// handle popupWindow click event
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// do anything when popupWindow was clicked
pw.dismiss(); // dismiss the window
}
});
// dismiss the popup window after 3sec
new Handler().postDelayed(new Runnable() {
public void run() {
pw.dismiss();
}
}, 3000);
}
xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical"
android:elevation="10dp"
android:padding="20dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#FFF"
android:textStyle="bold"/>
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#FFF"/>
</LinearLayout>
你是对的,一个Toast
对象没有办法与之交互,但是有很多库可以给你和烤面包一样的外观和感觉,但有一些交互性。我使用的是https://github.com/JohnPersano/SuperToasts
“对话”类型的活动可能是您最好的选择。
在清单中:
<activity android:name=".ToastLikeActivity"
android:theme="@android:style/Theme.Dialog"
android:label="@string/label"
></activity>
并使 onCreate() 中的活动超时:
class ToastLikeActivity extends Activity {
@Override
public void onCreate(Bundle state)
// auto-kill activity after X seconds <-------------------------
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
ToastLikeActivity.this.finish(); // kill after X seconds
}
}
}, VisibleTimeSecs*1000);
}
要显示对话框,请像使用任何其他活动一样启动它:
Intent i = new Intent(this, ToastLikeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
它会在 X 秒后出现并自动消失。
这样的弹出窗口不会与调用者活动相关联。事实上 - 它甚至不需要调用者活动。您甚至可以通过服务激活它(坏主意,但可能)。
您可以对 ToastLikeActivity 实现基本上任何类型的敏感(即接受用户的点击)接口。特别是:你可以让它的外观透明,让它看起来像对话。
我认为你需要的实际上是一个PopupWindow
可以在这里看到的“ http://developer.android.com/reference/android/widget/PopupWindow.html ”。
Toasts 有一个非常具体的任务,就是通知用户,而不需要他们的任何输入。因此,与其尝试扩展 Toast 的用途,不如使用用户可以与之交互的 PopupWindow。