自定义操作栏按钮编码疑点

时间:2014-04-30 06:57:27

标签: android

我制作了一个自定义操作栏:

enter image description here

现在我想编写我添加的按钮的代码 喜欢回家,回来,前进等。

那么我要创建一个新的java文件来编写代码吗? 或者我在MainActivity中编写点击事件代码? 我还需要对其他文件(如清单或MainActivity)进行任何其他更改吗?

我的MainActivity.java:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button click = (Button) findViewById(R.id.button1);
    click.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent launchactivity = new Intent(MainActivity.this,
                    potraitWebview.class); // **customactionbar is used in this activity**
            startActivity(launchactivity);
            finish();
        }
    });
}

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

这里是im使用customactionbar

的活动代码
public class potraitWebview extends Activity {
WebView web;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.potraitlayout);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
            R.layout.customtitlebar);
.
.
}

1 个答案:

答案 0 :(得分:-1)

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.custom, menu);//Another xml file name

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        switch(id)
        {
        case R.id.backwardId:
            Intent back=new Intent(getApplicationContext(),BackwardActivity.class);
            startActivity(back);
            return true;
        case R.id.Home:
            Intent home=new Intent(getApplicationContext(),HomeActivity.class);
        startActivity(home);
        return true;
        case R.id.ForwardId:
            Intent forward=new Intent(getApplicationContext(),ForwardActivity.class);
            startActivity(forward);
            return true;
            default:
                return super.onOptionsItemSelected(item);

        }