java 如何使用浮动操作按钮 (fab) 创建新活动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32835214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:51:16  来源:igfitidea点击:

How to Create new activity using float action button (fab)?

javaandroidandroid-intentandroid-activity

提问by Cliff

Hi can somebody have a suggestions on how to make a new activity using Fab like if you click the fab button it will show up a side transition saying hello world? actually i am creating a chat app and i want to replace the "hello world" with my contacts.

嗨,有人可以就如何使用 Fab 制作新活动提出建议,就像您单击 fab 按钮一样,它会显示一个侧面过渡,说 hello world?实际上我正在创建一个聊天应用程序,我想用我的联系人替换“hello world”。

i got a reference link or source code regarding the float action button i mention.what i did is i removed the list view so that only the fab button is on the screen, http://www.myandroidsolutions.com/2015/01/01/android-floating-action-button-fab-tutorial

我得到了一个关于我提到的浮动操作按钮的参考链接或源代码。我所做的是我删除了列表视图,以便屏幕上只有 fab 按钮, http://www.myandroidsolutions.com/2015/01/ 01/android-floating-action-button-fab-tutorial

any comments or suggestion will be helpful for me to execute this. thanks!

任何意见或建议都将有助于我执行此操作。谢谢!

回答by Lakhwinder Singh

Just put the intent to your new activity in

只需将意图放在您的新活动中

fabImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //here
                startActivity(new Intent(YourCurrentActivity.this,NewActivity.class));
            }
        });

回答by Yani2000

@warlock Your answer didn't work entirely for me.
If I pass thisin Intent I get an error in Studio that it can't resolve the constructor: Intent(View.OnClickListener, Class<NewActivity>).
I had to pass the upper Activity class into Intent or fab's View, like this:

@warlock 你的回答并不完全适合我。
如果我传入thisIntent,我会在 Studio 中收到一个错误,提示它无法解析构造函数:Intent(View.OnClickListener, Class<NewActivity>)
我必须将上层 Activity 类传递到 Intent 或 fab 的视图中,如下所示:

fabImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //here
                startActivity(new Intent(MainActivity.this, NewActivity.class));
                // or:
                startActivity(new Intent(v.getContext(), NewActivity.class));
            }
        });

回答by PAWAN LAKHOTIA

FloatingActionButton floatingActionButton=(FloatingActionButton)rootView.findViewById(R.id.fab);
    floatingActionButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view){
            startActivity(new Intent(view.getContext(), NextActivity.class));
        }
    });

This works absolutely fine.

这工作得很好。

回答by Omar Mardini

you should define your activity in androidmainfest.xml as

您应该在 androidmainfest.xml 中将您的活动定义为

<activity android:name=".MainActivity">
        </activity>