android - 跨所有活动的浮动操作按钮,每个活动具有不同的实现
问题描述
我希望在我的应用程序的每个活动中都有以下浮动操作按钮:
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" />
但它一直在崩溃。我忘了做什么?
另外,我想知道如何对按钮进行编程以根据单击的活动显示不同的消息。
任何想法将不胜感激。谢谢你。
解决方案
推荐阅读
- reactjs - 如何在反应本机应用程序中翻译来自服务器的消息
- laravel - 无法创建 Laravel 关系
- security - .net core 中的 asp.net webform 替换 Session 是什么?
- react-redux - 在事件处理函数中传递值后,动作调度不起作用
- r - 根据分位数的接近程度更改向量的值
- java - Java - ImageIcon 不重新加载更新文件
- php - Wordpress CF7 根据所选主题直接发送电子邮件
- nodemailer - 我们可以使用 nodemailer 将动态加载的 html 文件作为电子邮件发送吗
- redirect - cmd.exe 和重定向的有趣观察
- c++ - C++ ifstream XCode / VSCode