Android 向上按钮不起作用 [英] Android Up button not working

查看:39
本文介绍了Android 向上按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试手动实现在按下操作栏上的向上按钮时必须执行的操作,但由于某种原因,当我按下它时没有任何反应.

I am trying to manually implement the actions that must take place when the up button on the actionbar is pressed but for some reason nothing happens when I press it.

这是我的代码:

public class ActivityOne extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_one);
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar_actionbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        Button button = (Button)findViewById(R.id.btn1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openActivityTwo();
            }
        });
        Button button2 = (Button)findViewById(R.id.btn2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openActivityThree();
            }
        });
    }


    void openActivityTwo(){
        Intent intent = new Intent(this, ActivityTwo.class);
        startActivity(intent);
    }

    void openActivityThree(){
        Intent intent = new Intent(this, ActivityThree.class);
        startActivity(intent);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_activity_one, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        else if(id == R.id.homeAsUp){
            Log.i("","Up is pressed");
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

我知道我必须为活动明确分配一个父活动我想在清单文件上实现向上导航,但问题是这个活动有多个父活动,所以我想调用 finish() 在此活动上按下向上按钮时的方法将是更好的方法.

I understand that I have to explicitly assign a parent activity for the activity I want to implement up navigation on the manifest file, but problem is that this activity has multiple parents so I thought calling the finish() method when the up button is pressed on this activity will be the better approach.

我已经尝试过 id == R.id.homeid == R.id.homeAsUp 并且它们都不起作用.不知道是不是因为我用的是AppCompactActivity 还是什么请帮忙

I have already tried both id == R.id.home and id == R.id.homeAsUp and they both do not work. I do not know if it is because I am using AppCompactActivity or what Please help

推荐答案

这是如何实现的,试试这个代码

Here is how you can implement this, try this code

使用 android.R.id.home 而不是 R.id.home 或 R.id.homeAsUp

public boolean onOptionsItemSelected(final MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                //use onBackPressed() OR finish();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

这篇关于Android 向上按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆