Activity間および通常ボタンを起動するためのクリックイベントについて
16976 ワード
2つのjavaファイルは2つのlayoutに対応します
メインActivityボタン起動サブActivityサブActivity複数ボタン起動メインActivity
プライマリActicity
1 package lianxi;
2
3 import com.example.jichu_lianxi.R;
4
5 import android.app.Activity;
6 import android.content.Intent;
7 import android.os.Bundle;
8 import android.view.View;
9 import android.view.View.OnClickListener;
10 import android.widget.Button;
11
12 public class Mainactivity extends Activity{
13 private Button button_open; //
14 @Override
15 protected void onCreate(Bundle savedInstanceState) {
16 // TODO Auto-generated method stub
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.mainactivity);
19 button_open = (Button) findViewById(R.id.button_open);//
20 button_open.setOnClickListener(new OnClickListener() { //
21
22 @Override
23 public void onClick(View v) {
24 // TODO Auto-generated method stub
25 Intent intent1 = new Intent(); // Activity
26 intent1.setClass(Mainactivity.this, NewActivity.class);
27 // Activity , Activity, Activity
28 startActivity(intent1);// Activity
29 }
30 });
31 }
32 }
副Activity
1 package lianxi;
2
3 import com.example.jichu_lianxi.R;
4
5 import android.app.Activity;
6 import android.content.Intent;
7 import android.os.Bundle;
8 import android.view.View;
9 import android.view.View.OnClickListener;
10 import android.widget.Button;
11
12 public class NewActivity extends Activity implements OnClickListener{
13 private Button button_1; //
14 private Button button_2;
15 private Button button_3;
16 @Override
17 protected void onCreate(Bundle savedInstanceState) {
18 // TODO Auto-generated method stub
19 super.onCreate(savedInstanceState);
20 setContentView(R.layout.newactivity);
21 button_1 = (Button) findViewById(R.id.button_1); //
22 button_2 = (Button) findViewById(R.id.button_2);
23 button_3 = (Button) findViewById(R.id.button_3);
24
25 button_1.setOnClickListener(this); //
26 button_2.setOnClickListener(this);
27 button_3.setOnClickListener(this);
28
29 }
30 @Override
31 public void onClick(View v) { //
32 // TODO Auto-generated method stub
33 switch (v.getId()) {
34 case R.id.button_1:
35 Intent intent1 = new Intent();
36 // Activity
37 intent1.setClass(NewActivity.this, Mainactivity.class);
38 // Activity
39 startActivity(intent1);
40 // Activity
41
42 break;
43 case R.id.button_2: // button_1
44 break;
45 case R.id.button_3:// button_1
46 break;
47 }
48 }
49 }
メインActivity対応layout
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <Button
8 android:id="@+id/button_open"
9 android:layout_width="wrap_content"
10 android:layout_height="wrap_content"
11 android:text="@string/open" />
12
13 </LinearLayout>
副Activity対応layout
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <Button
8 android:id="@+id/button_1"
9 android:layout_width="wrap_content"
10 android:layout_height="wrap_content"
11 android:text=" Activity" />
12
13 <Button
14 android:id="@+id/button_2"
15 android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:text=" Activity" />
18
19 <Button
20 android:id="@+id/button_3"
21 android:layout_width="wrap_content"
22 android:layout_height="wrap_content"
23 android:text=" Activity" />
24
25 </LinearLayout>
AndroidManifest.xmlファイルコード:
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2 package="com.example.jichu_lianxi"
3 android:versionCode="1"
4 android:versionName="1.0" >
5
6 <uses-sdk
7 android:minSdkVersion="8"
8 android:targetSdkVersion="18" />
9
10 <application
11 android:allowBackup="true"
12 android:icon="@drawable/ic_launcher"
13 android:label="@string/app_name"
14 android:theme="@style/AppTheme" >
15 <activity android:name="lianxi.Mainactivity">
16 <intent-filter
17 >
18 <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER" />
19 </intent-filter>
20 </activity>
21 <activity android:name="lianxi.NewActivity"></activity>
22 </application>
23
24 </manifest>
16~19行このActivityをプライマリActivityとして設定し、起動バーに表示
21行
プロジェクトにAactivityを追加するたびに、このxml宣言の下に