Activity介绍
1.Activity使用方法(跳转):
(1)写Activity类继承Activity
package com.example.test2; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second_activity); } }
(2)写布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is second activity" /> </LinearLayout>
(3) AndroidManifest.xml注册Activity
(4)MainActivity中通过一个按钮的点击事件引出第二个Activity
全部代码:
package com.example.test2; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; public class MainActivity extends Activity implements OnCheckedChangeListener { private Button login_button; private Button reset; private RadioGroup rg; private Button other; private CheckBox checkBox1; private Button myButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_radiobutton); rg = (RadioGroup) this.findViewById(R.id.radioGroup1); rg.setOnCheckedChangeListener(this); Button btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this,SecondActivity.class); MainActivity.this.startActivity(intent); } }); } @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; } @Override public void onCheckedChanged(RadioGroup arg0, int checkId) { // TODO Auto-generated method stub switch(checkId){ case R.id.radio0: Log.i("tag", "当前选中男"); break ; case R.id.radio1: Log.i("tag", "当前选中女"); break ; default: break ; } } }
(5)效果:
【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】