4

设置以下使主页图标显示为按下状态:

actionBar.setHomeButtonEnabled(true);

但是,设置 OnClickListener 后,主页图标停止显示按下状态:

ImageView iconImage = (ImageView) activity.findViewById(android.R.id.home);
iconImage.setOnClickListener(new android.view.View.OnClickListener() {
    @Override
    public void onClick(View v) {
    }
});

关于如何防止禁用新闻状态的任何想法?

4

2 回答 2

8

处理home图标的点击,不需要设置onClickListener,需要做以下..

public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() == android.R.id.home) { //app icon in action bar clicked; go back
        //do something
        return true;
    }

    return super.onOptionsItemSelected(item);
}
于 2012-11-29T09:50:47.690 回答
0
public class MainActivity extends Activity {

     Fragment fr;
      FragmentManager fm;
      Button btn1,btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        btn1=(Button)findViewById(R.id.button1);
        btn2=(Button)findViewById(R.id.button2);
        fm = getFragmentManager();


        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                     fr = new frag22();
                     FragmentTransaction ft = fm.beginTransaction();
                     ft.replace(R.id.fragment, fr);
                     ft.commit();

            }
        });
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);
    // TODO Auto-generated method stub
    return super.onCreateOptionsMenu(menu);
}

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

          if(item.getItemId() == android.R.id.home) { //app icon in action bar clicked; go back
                //do something

              Toast.makeText(getApplicationContext(), "set click", Toast.LENGTH_LONG).show();
                return true;
            }


        return super.onOptionsItemSelected(item);
    }
}
于 2015-04-29T06:24:48.260 回答