1.Activity02布局代码main.xml

<?xml version="1.0" encoding="utf-8"?> <!-- 该文件是Activity02.java所使用的布局文件 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 下面的标签声明了一个Buttin(按钮)控件,并未这个控件设置了ID,这个ID会被注册到R.java文件当中 --> <Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>

2.OtherActivity布局代码other.xml

<?xml version="1.0" encoding="utf-8"?> <!-- 该文件是OtherActivity.java文件所使用的布局文件 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/myTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>

3.Activity02.java

package mars.activity02; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /** * 这个Activity是应用程序启动只有运行的第一个Activity,在这个Activity当中有一个按钮 * @author mars_chenchuan * */ public class Activity02 extends Activity { /** Called when the activity is first created. */ //代表按钮对象的引用 private Button myButton = null; //复写父类当中的onCreate方法,Activity第一次运行时会调用这个方法 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //为Activity设置布局管理文件 setContentView(R.layout.main); //以下两行代码是根据控件的ID来得到控件对象 myButton = (Button)findViewById(R.id.myButton); //为按钮对象设置监听器对象 myButton.setOnClickListener(new MyButtonListener()); } //以下是一个内部类,这个内部类的对象是一个监听器(如果大家对监听器不是很熟悉,可以参考设计模式当中的观察者模式) class MyButtonListener implements OnClickListener{ //生成该类的对象,并将其注册到控件上。如果该控件被用户按下,就会执行onClick方法 @Override public void onClick(View v) { //生成一个Intent对象 Intent intent = new Intent(); //在Intent对象当中添加一个键值对 intent.putExtra("testIntent", "123"); //设置Intent对象要启动的Activity intent.setClass(Activity02.this, OtherActivity.class); //通过Intent对象启动另外一个Activity Activity02.this.startActivity(intent); /**以下的4行代码将启动发送短信的Activity,详细的情况会在以后的课程当中进行介绍 Uri uri = Uri.parse("smsto://0800000123"); Intent intent = new Intent(Intent.ACTION_SENDTO, uri); intent.putExtra("sms_body", "The SMS text"); startActivity(intent); */ } } }

4.OtherActivty.java

package mars.activity02; import android.app.Activity; import android.content.Intent; 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); //取得从上一个Activity当中传递过来的Intent对象 Intent intent = getIntent(); //从Intent当中根据key取得value String value = intent.getStringExtra("testIntent"); //根据控件的ID得到响应的控件对象 myTextView = (TextView)findViewById(R.id.myTextView); //为控件设置Text值 myTextView.setText(value); } }

源代码详见:

http://download.csdn.net/source/3169414

更多相关文章

  1. android 遍历所有控件
  2. Android学习--动态向SPinner控件中添加数据
  3. Android在代码中设置控件的drawableLeft,drawableRight,drawableTo
  4. 编程回忆之Android回忆(个性化控件(View)篇)
  5. Android 控件布局实现卡片效果,阴影效果
  6. 使用eclipse与android studio 在开发自定义控件时的区别
  7. 介绍一个Android的开源的自定义小控件:TextDrawable
  8. Android常见的十个高级控件

随机推荐

  1. android实现SharedPreferences的存入与取
  2. android:ImageView,访问网上图片并显示出
  3. android skia 使用实例
  4. 【Android】viewpager banner 广告 自动
  5. android 8.0 notification
  6. android ontouch onclick 触发顺序
  7. android RadioButton 点击时候出现点击声
  8. android 屏幕适配之自动生成多重values
  9. android 下载文件图片圆形进度条
  10. Ubuntu下android真机调试Using Hardware