Activity与Intent的案例
实现的效果:点击按钮跳转到另一个Activity
首先把默认的布局中的TextView改成Button
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
在Activity上添加Button事件
package fengda.activity02;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Activity02 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity02);
Button button = (Button) findViewById(R.id.myButton);
button.setText("点我,我就跳!!");
button.setOnClickListener( new MyButtonListener());
}
class MyButtonListener implements OnClickListener {
public void onClick(View v) {
//建立一个空的Intent,用来传递数据
Intent myIntent = new Intent();
myIntent.setClass(Activity02.this, Activity02_1.class);
myIntent.putExtra("sendValue", "我是被传过来的!!!");
Activity02.this.startActivity(myIntent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_activity02, menu);
return true;
}
}
package fengda.activity02;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Activity02_1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity02_1);
Intent myIntent = getIntent();
TextView myTextView = (TextView) findViewById(R.id.myTextView);
myTextView.setText(myIntent.getStringExtra("sendValue"));
}
}
Activity02的布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fengda.activity02"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Activity02"
android:label="@string/title_activity_activity02" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity02_1"
android:label="@string/title_activity_activity02_1" >
</activity>
</application>
</manifest>
效果调出来了
需要注意的问题在XML中最好不要直接使用中文,如果Activity label中有中文,会导致无法生成标
还在在Win7上使用Eclipse20120920-0800感觉好卡
调了好长时间没有解决一个问题,实在找不到怎么解决了
一直只提示这个错误,虽然不影响程序运行,但却怎么也找不到哪里出的问题
11-01 18:32:22.511: E/Trace(817): error opening trace file: No such file or directory (2)