如何在自定义工具栏中添加返回按钮是最佳方法?

huangapple go评论85阅读模式
英文:

What is the best way to add a back button to custom toolbars?

问题

我知道如何在我的自定义工具栏中添加一个后退导航按钮,然而必须有一种更有效的方法来将这个功能添加到多个活动中,而不是在每个活动中复制并粘贴这段代码...

        Toolbar toolbar = findViewById(R.id.tlbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

我尝试过创建一个通用类,其中我创建了一个可以在每个活动中调用的函数,然而你需要使用 findViewById,而这只能在活动中完成。关于此问题,有什么建议吗?

英文:

So i know how to add a backwards navigation button to my custom toolbar, however there must be a more efficient way to add the functionality to multiple activities than copying and pasting this code in each one...

        Toolbar toolbar = findViewById(R.id.tlbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

I've tried making a common class were i create a function that could be called in each activity however you need to findViewById which can only be done in the activity. Any suggestions around this?

答案1

得分: 1

首先创建一个基类如下所示:

public class BaseActivity extends AppCompatActivity {

    public void setToolbar(@IdRes int toolbarID) {
        try {
            Toolbar toolbar = findViewById(toolbarID);
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

然后将所有的Activity类继承BaseActivity类:

public class LoginNewActivity extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbar(R.id.toolbar); //You can call your method like this
    }
}

如果你想了解更多关于这个主题的内容,可以搜索 Inheritance and polymorphism in JAVA

英文:

First Create a base class like this

public class BaseActivity extends AppCompatActivity{


    public  void setToolbar(@IdRes int toolbarID){
        try {
            Toolbar toolbar = findViewById(toolbarID);
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }catch (Exception e){
            e.printStackTrace();

        }

    }

}

After extends all Activity class with BaseActivity class

public class LoginNewActivity extends BaseActivity{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbar(R.id.toolbar); //You can call your method like this
    }
}

If you want to know more about this do search for Inheritance and polymorphism in JAVA

答案2

得分: 0

在您的清单中的activity标签上添加parentactivityname,并将值添加为您希望在返回按钮按下时转到的活动,这是对您最好的解决方案。

英文:

in your activity tag on manifest add parentactivityname and add value as an activity which you want to go when back pressed.this is the best solution for you.

huangapple
  • 本文由 发表于 2020年4月9日 17:11:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/61117686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定