今回はPreferenceScreenをクリックすると、別のアクティビティを開きます。
Intentを使って別のアクティビティを開く方法は下記を見てください。
Android 別のアクティビティを開く(戻り値を受け取らない)
Android 別のアクティビティを開く(戻り値を受け取る)
まず、PreferenceScreenをクリックしたときに、開くアクティビティを作成します。
作り方はコチラAndroid プロジェクトに新しいアクティビティを追加する
Activityを継承し、TextViewを1つ配置しただけの「SubActivity」を用意しました。
res/layout/sub.xml(SubActivityのレイアウト)
<?xml version="1.0" encoding="utf-8"?>
<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:text="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/label">
</TextView>
</LinearLayout>
SubActivity.javapackage my.study.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SubActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sub);
//受け取った値をTextViewに表示する
TextView label = (TextView)findViewById(R.id.label);
Intent intent = getIntent();
Bundle extra = intent.getExtras();
String str = null;
if (extra != null){
str = extra.getString("arg1") + "\n";
if (extra.containsKey("arg2")){
str += extra.getString("arg2");
}
}
label.setText(str);
//遷移元に値を返す。
Intent result = new Intent();
result.putExtra("result", "From SubActivity Result");
setResult(RESULT_OK,result);
}
}
PreferenceActivityを継承したMainActivityから、PreferenceScreenをクリックしたときに別のアクティビティを開く方法はいくつかあります。
まずはPreferenceScreenのxml定義にintentを記述する方法です。
intentタグのtargetPackageにパッケージ名を指定し、targetClassに遷移するアクティビティを指定します。
extraタグで遷移するアクティビティに値を渡す事ができます。
res/xml/mainpref.xml(MainActivityのレイアウト)
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen
android:key="@string/prefScreen1"
android:summary="xmlでIntentを飛ばす"
android:title="PreferenceScreen1" >
<intent
android:action="android.intent.action.MAIN"
android:targetPackage="my.study.android"
android:targetClass="my.study.android.SubActivity" >
<extra android:name="arg1" android:value="prefscreen1: arg1 = from xml" />
</intent>
</PreferenceScreen>
</PreferenceScreen>
遷移するアクティビティに渡す値をコードから追加する場合は、PreferenceScreen#getIntent()メソッドでIntentオブジェクトを取得しIntent#putExtra()メソッドで値を追加します。
MainActivity.java
package my.study.android;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
public class MainActivity extends PreferenceActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//xmlレイアウトの読み込み
this.addPreferencesFromResource(R.xml.mainpref);
//PreferenceScreen1
CharSequence csScreenPref1 = getText(R.string.prefScreen1);
PreferenceScreen screenPref1 = (PreferenceScreen)findPreference(csScreenPref1);
Intent intent1 = screenPref1.getIntent();
intent1.putExtra("arg2", "prefscreen1: arg2 = from code");
}
}
次はPreferenceScreenにコードでintentを記述し設定する方法です。
res/xml/mainpref.xml(MainActivityのレイアウト)
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen
android:summary="コードでIntentを設定する"
android:title="PreferenceScreen2"
android:key="@string/prefScreen2">
</PreferenceScreen>
</PreferenceScreen>
intentオブジェクトを作成し、PreferenceScreen#setIntent()メソッドでPreferenceScreenに設定します。MainActivity.java
package my.study.android;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
public class MainActivity extends PreferenceActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//xmlレイアウトの読み込み
this.addPreferencesFromResource(R.xml.mainpref);
//PreferenceScreen2
CharSequence csScreenPref2 = getText(R.string.prefScreen2);
PreferenceScreen screenPref2 = (PreferenceScreen)findPreference(csScreenPref2);
Intent intent2 = new Intent(this,SubActivity.class);
intent2.putExtra("arg1", "prefscreen2: arg1 = from code");
screenPref2.setIntent(intent2);
}
}
この2つの方法は遷移先のアクティビティに値を渡すことができますが、遷移先のアクティビティから値を受け取ることができません。
遷移先のアクティビティから戻ってきたときはonResume()が発生します。
次はPreferenceScreenのoOnPreferenceClickListenerをハンドルし、startActivityForResultでアクティビティを開く方法です。
この方法はonActivityResult()が発生し、遷移先のアクティビティから値を受け取ることができます。
もちろんonResume()も発生します。
res/xml/mainpref.xml(MainActivityのレイアウト)
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen
android:summary="onClickでIActivityを開く"
android:title="PreferenceScreen3"
android:key="@string/prefScreen3">
</PreferenceScreen>
</PreferenceScreen>
MainActivity.javapackage my.study.android;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.Toast;
public class MainActivity extends PreferenceActivity {
private static final int SUBACTIVITY = 1;
private PreferenceScreen _screenPref3;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//xmlレイアウトの読み込み
this.addPreferencesFromResource(R.xml.mainpref);
//PreferenceScreen3
CharSequence csScreenPref3 = getText(R.string.prefScreen3);
_screenPref3 = (PreferenceScreen)findPreference(csScreenPref3);
_screenPref3.setOnPreferenceClickListener(new OnPreferenceClickListener(){
@Override
public boolean onPreferenceClick(Preference pref) {
return screenPref3_onPreferenceClick(pref);
}});
}
private boolean screenPref3_onPreferenceClick(Preference pref){
Intent intent = new Intent(this,SubActivity.class);
intent.putExtra("arg1", "prefscreen3: arg1 = from code");
startActivityForResult(intent, SUBACTIVITY);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Toast.makeText(this, "onActivityResult", Toast.LENGTH_SHORT).show();
if (requestCode == SUBACTIVITY){
String summary = null;
if (resultCode == RESULT_OK){
Bundle extras = intent.getExtras();
if (extras != null){
summary = "result:" + extras.getString("result");
}
}
_screenPref3.setSummary(summary);
}
}
}
| MainActivity |
| PreferenceScree1をクリック後 |
| PreferenceScree2をクリック後 |
| PreferenceScree3をクリック後 |
| MainActivityに戻る |
【関連項目】
Android プリファレンスの編集
Android アプリ設定画面を作成する
Android アプリ設定画面を作成する CheckBoxPreference
Android アプリ設定画面を作成する EditTextPreference
Android アプリ設定画面を作成するRingtonePreference
Android アプリ設定画面を作成するListPreference
Android アプリ設定画面を作成する 依存関係を設定する
Android アプリ設定画面を作成する PreferenceScreen
Android アプリ設定画面を作成する PreferenceScreen(別アクティビティ)
0 件のコメント:
コメントを投稿