【问题标题】:Actionbar back button not working in android操作栏后退按钮在android中不起作用
【发布时间】:2014-04-01 15:10:23
【问题描述】:

我正在创建一个 android 应用程序,我需要在其中放回导航,以便当用户单击操作栏的主页按钮时,用户应该在我的应用程序上移回一个屏幕。但是操作栏主页按钮不响应点击事件. 这是我的代码。

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }
            @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);

        ActionBar actionBar = getActivity().getActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);


        return rootView;
    } }

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.home:
        Toast.makeText(getApplication(), "Back", Toast.LENGTH_LONG).show();

        break;

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

【问题讨论】:

  • 您是否尝试过从Activity 执行此操作?
  • 是的。但它没有响应。
  • 另外,在您的case R.id.home 中,它实际上应该是android.R.id.home
  • 好的。谢谢你。我已将案例 R.id.home 更改为案例 android.R.id.home。现在它的工作。 @hiphopdroid
  • 但我不明白为什么我们使用 case android.R.id.home 而不是 case R.id.home ? @hiphopdroid

标签: android android-actionbar


【解决方案1】:
case android.R.id.home:

尝试用这个代替case R.id.home:

【讨论】:

    【解决方案2】:

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

    public boolean onOptionsItemSelected(MenuItem item) {
    
        switch (item.getItemId()) {
        case android.R.id.home:
            Toast.makeText(getApplication(), "Back", Toast.LENGTH_LONG).show();
    
            break;
    
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-24
      • 1970-01-01
      • 2014-12-27
      • 1970-01-01
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      相关资源
      最近更新 更多