独立Activi的跳转:
首先要建立一个监听器:
·class MyButtonlistener implements android.view.View.OnClickListener{
@Override //重写onClick方法
public void onClick (View v) {
//ToDO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Activity02.this, OtherActivity.class);
Activity02.this.startActivity(intent);
}
}
}
通过Ientent来传递信息。
注意借口为android.view.View.OnClickListener而不是OnClickListener否则在重写onClick方法是会出现参数设置不正确。
myButton.setOnClickListener(new MyButtonListener()); 把监听器与按钮结合,点击按钮实现Activity的跳转。其中myButton是定义的私有成员。 MyButtonListener()是监听器类名。
实现简单的Activity的跳转源码:
package project.Activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity02 extends Activity {
/** Called when the activity is first created. */
private Button myButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button)findViewById(R.id.myButton); //实现按钮
myButton.setOnClickListener(new MyButtonlistener());
}
class MyButtonlistener implements android.view.View.OnClickListener{
@Override
public void onClick (View v) {
//ToDO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Activity02.this, OtherActivity.class);
Activity02.this.startActivity(intent);
}
}
}
跳转到的Activity的Java代码:
package project.Activity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class OtherActivity extends Activity{
private TextView myTextView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setText(R.string.other);
}
}
只是类名(public class OtherActivity extends Activity)和调用的布局(setContentView(R.layout.other);)文件发生了变化。
首先要建立一个监听器:
·class MyButtonlistener implements android.view.View.OnClickListener{
@Override //重写onClick方法
public void onClick (View v) {
//ToDO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Activity02.this, OtherActivity.class);
Activity02.this.startActivity(intent);
}
}
}
通过Ientent来传递信息。
注意借口为android.view.View.OnClickListener而不是OnClickListener否则在重写onClick方法是会出现参数设置不正确。
myButton.setOnClickListener(new MyButtonListener()); 把监听器与按钮结合,点击按钮实现Activity的跳转。其中myButton是定义的私有成员。 MyButtonListener()是监听器类名。
实现简单的Activity的跳转源码:
package project.Activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity02 extends Activity {
/** Called when the activity is first created. */
private Button myButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button)findViewById(R.id.myButton); //实现按钮
myButton.setOnClickListener(new MyButtonlistener());
}
class MyButtonlistener implements android.view.View.OnClickListener{
@Override
public void onClick (View v) {
//ToDO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Activity02.this, OtherActivity.class);
Activity02.this.startActivity(intent);
}
}
}
跳转到的Activity的Java代码:
package project.Activity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class OtherActivity extends Activity{
private TextView myTextView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setText(R.string.other);
}
}
只是类名(public class OtherActivity extends Activity)和调用的布局(setContentView(R.layout.other);)文件发生了变化。