首页 > 解决方案 > 跨所有活动的浮动操作按钮,每个活动具有不同的实现

问题描述

我希望在我的应用程序的每个活动中都有以下浮动操作按钮:

private void questionButton()  {

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab2);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            AlertDialog.Builder builder = new AlertDialog.Builder(NewsHeadlines.this);
            builder.setTitle("Do you want to delete this?");

            //what is the message
            builder.setMessage("Do something").setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    finish();
                    Toast.makeText(getApplicationContext(),"You chose yes", Toast.LENGTH_SHORT).show();
                }
            })
                    .setNegativeButton("No", new DialogInterface.OnClickListener()  {
                        public void onClick(DialogInterface dialog, int id)  {
                            finish();
                            Toast.makeText(getApplicationContext(),"you chose no", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .create().show();
        }
    });

}

和xml:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/question_mark"
    android:layout_margin="16dp" />

我尝试将此代码写入抽象的 BaseActivity 类,然后让我的其他类扩展 BaseActivity。我确保将基本活动布局包含在所有其他活动中:

<include layout="@layout/activity_base" />

但它一直在崩溃。我忘了做什么?

另外,我想知道如何对按钮进行编程以根据单击的活动显示不同的消息。

任何想法将不胜感激。谢谢你。

标签: androidinheritanceabstract-classfloating-action-buttonextends

解决方案


推荐阅读