看了两节Android的视频教程。今天终于完成了第一个Android工程!
1.建立了两个Activity,第一个是ActivityMain,第二个是OtherActivity
2.在AndroidMainFest.xml中对这两个Activity进行注册
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ActivityMain"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".OtherActivity"></activity>
</application>
3.在res的layout中建立main.xml跟other.xml的的那个布局文件。在main.xml中放置1个TextView跟Button,然后在other.xml中放置1个TextView
4.在ActivityMain类中声明button的onclick事件并实现,建立1个intentMean方法,通过Intent,把“myMean” putExtra到OtherActivity中去
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
intentMean();
}
});
public void intentMean(){
Intent intent=new Intent();
intent.setClass(this, OtherActivity.class);
intent.putExtra("myMean", "Hello!\nMy Mean,Do You Konw?");
this.startActivity(intent);
}
5.在OtherActivity中获得传递过来的myMean并显示出来
TextView showView=(TextView)this.findViewById(R.id.showTextView);
String myMean=this.getIntent().getExtras().getString("myMean");
showView.setText(myMean);