有人可以帮助我尝试onclick android教程如何使用按钮更改页面,但我卡住我得到一个错误,说明Main.Activity.b1字段的值不使用我无法弄明白
package com.example.lasttry;
package com.example.lasttry;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
public class MainActivity extends Activity implements OnClickListener {
private Button b1;
public final static String EXTRA_MESSAGE = "com.example.lasttry";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setContentView(R.layout.activity_main);
this.b1 = (Button)this.findViewById(R.id.b1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View v) {
finish(); // TODO Auto-generated method stub
}
public void sendMessage(View v){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText= (EditText) findViewById(R.id.b1);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
答案 0 :(得分:0)
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), yourSecondActivityClass.class);
startActivity(i);
}
});
您可以将此代码放入onCreate方法。因此,只要用户单击该按钮,您的应用就会根据您的SecondActivityClass打开一个新的活动实例。此类必须是Activity的类型,您必须在Manifest文件中定义此活动。