2014-10-09 94 views
-2

Iam新的Android和试图改变与按下按钮的窗口。我想下面的代码会工作,但是当我添加Android - 如何更改窗口?

setContentView(R.layout.layout2) 

;, (Button) findViewById(R.id.imageButton);突然变成红色下划线与文字意外投给按钮:布局标签是ImageButton的

final Button button = (Button) findViewById(R.id.imageButton); 
      button.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        setContentView(R.layout.layout2); 
       } 
      }); 

回答

0

创建按钮变量:

private Button home; 

在你的onCreate,声明它:

home = (ImageView) findViewById(R.id.nav_home); 
home.setOnClickListener(this); 

那么你的onClick应该是这样的:

public void onClick(View v) { 
    startActivity(new Intent("com.example.project.activityName")); 
}