一.
创建Activity的要点:
1.继承Activity类
2.需要重写onCreate方法
3.需要在AndroidManifest.xml注册
4.为Activity添加控件和内容
5.setContentView(R.layout.activity_main);设置布局文件
6.TextView
myTextView=(TextView)findViewById(R.id.myTextView);获取控件
二、
Intent
1.Component
name
2.Action
3.Data
4.Category
5.Extras
6.Flags
通过Intent跳转Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button) findViewById(R.id.myButton); myButton.setOnClickListener(new MyButtonListener()); } class MyButtonListener implements OnClickListener { @Override public void onClick(View v) { /*// 1.跳转到本程序的Activity * Intent intent=new Intent(); * intent.putExtra("key", "DATA"); * intent.setClass(MainActivity.this, OtherActivity.class); * MainActivity.this.startActivity(intent); */
//2.跳转到外部Activity 如:发送短信
Uri uri = Uri.parse("smsto://0800000123"); Intent intent = new Intent(Intent.ACTION_SENDTO, uri); intent.putExtra("sms_body", "The SMS text"); startActivity(intent); } }
public class OtherActivity extends Activity{
private TextView myTextView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
Intent
intent=getIntent();//获取Intent
String
value=intent.getStringExtra("key");
myTextView=(TextView)findViewById(R.id.textView1);
myTextView.setText(value);
}
}