表示されたアクティビティ ( など)に関連付けられておらず、クリック可能な最小限の侵入型の非ブロック通知を表示する必要があります。これが可能かどうかは誰にもわかりますか?残念ながら、通知 (カスタムまたはその他) はクリックできないようです (つまり、ビューに を設定しても効果がありません)。そして、私が間違っている場合は訂正してください。すべての選択肢 ( 、、) は、それが表示されたアクティビティに関連付けられた通知を表示します (つまり、アクティビティが終了すると表示されなくなります)。助言がありますか?Toast
Toast
OnClickListener
AlertDialg
PopupWindow
Crouton
4 に答える
を使用PopupWindow
して、 anonClickListener
を追加し、 aを追加しhandler
て、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 には、基本的にあらゆる種類の機密性の高い (つまり、ユーザーのクリックを受け入れる) インターフェイスを実装できます。特に:外観を透明にして、ダイアログのような外観にすることができます。