• 検索結果がありません。

android2.indd

N/A
N/A
Protected

Academic year: 2021

シェア "android2.indd"

Copied!
6
0
0

読み込み中.... (全文を見る)

全文

(1)

第 10 章サンプルコード集

この章ではプログラミングの参考となるサンプルコードを掲載しています。 コード記載のない部分についてはプロジェクトのデフォルトです。

アクティビティ間のデータ受け渡しサンプル

アプリケーション名: ActivityResultTest プロジェクト名: ActivityResultTest パッケージ名: com.example.activitytest 最小必須 SDK : API 10: Android 2.3.3 ターゲット SDK : API 18: Android 4.3 他デフォルト

アプリケーション仕様

メインアクティビティのエディットテキストにセットしたテキストをサブアクティビティに受け渡し、サブアクティ ビティのエディットテキストに表示。 サブアクティビティで編集後、そのテキストをメインアクティビティに返し、メインアクティビティのエディット テキストに表示。 スクリーンショット

(2)

ソースコード

MainActivity.java

package com.example.activityresulttest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText;

public class MainActivity extends Activity { // リクエストコードの定数

private static final int SUB_ACTIVITY = 1; // 共通変数定義

private EditText inputText; @Override

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main); // エディットテキスト初期化

inputText = (EditText) findViewById(R.id.editText1); // ボタン初期化とクリックリスナーの実装

Button btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO 自動生成されたメソッド・スタブ // サブアクティビティ起動インテント生成

Intent intent = new Intent(getApplicationContext(), SubActivity.class); // インテントにパラメーターをセット intent.putExtra("MAIN_INPUT_TEXT", inputText.getText() .toString()); // 戻り結果を受け取るアクティビティの呼出し startActivityForResult(intent, SUB_ACTIVITY); } }); } // アクティビティの戻り値を受け取るメソッド @Override

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

super.onActivityResult(requestCode, resultCode, intent); // リクエストコードを照合して処理するメソッド

if (requestCode == SUB_ACTIVITY) {

// リザルトコードを照合して処理するメソッド if (resultCode == RESULT_OK) {

// 戻り値にセットされたインテントのパラメータを受け取る Bundle extras = intent.getExtras();

(3)

// パラメータを受け取っていたらテキストエディットに表示、無かったら空白を表示 if (extras != null) { inputText.setText(extras.getString("SUB_TEXT")); } else { inputText.setText(""); } } } } }

SubActivity.java

package com.example.activityresulttest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText;

public class SubActivity extends Activity { // 共通変数定義

private EditText editText; @Override

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.activity_sub); // エディットテキスト初期化

editText = (EditText) findViewById(R.id.sub_editText1); // 受け渡されたインテントのパラメータを取得

Intent intent = getIntent(); Bundle extras = intent.getExtras();

// パラメータがある場合にはエディットテキストに表示、無かったら空白を表示 if (extras != null) { editText.setText(extras.getString("MAIN_INPUT_TEXT")); } else { editText.setText(""); } // キャンセルボタンの初期化とクリックリスナーの実装 Button btn_cancel = (Button) findViewById(R.id.btn_cancel); btn_cancel.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

(4)

setResult(RESULT_CANCELED); finish();

} });

// リターンボタンの初期化とクリックリスナーの実装 Button btn_return = (Button) findViewById(R.id.btn_return); btn_return.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// インテントを生成し、パラメーターにエディットテキストの内容をセット Intent intent = new Intent();

intent.putExtra("SUB_TEXT", editText.getText().toString()); // リザルトコードに OK をセットして戻り値をセットしてアクティビティを終了 setResult(RESULT_OK, intent); finish(); } }); } }

activity_main.xml

<LinearLayout 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" android:orientation="vertical" android:padding="5dp" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" アクティビティを起動する " /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="text" > <requestFocus /> </EditText> </LinearLayout>

(5)

activity_sub.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp" > <TextView android:id="@+id/sub_textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="text|phone" android:text=" サブアクティビティ " /> <EditText android:id="@+id/sub_editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="text" > <requestFocus /> </EditText> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btn_cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text=" キャンセル " /> <Button android:id="@+id/btn_return" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text=" リターン " /> </LinearLayout> </LinearLayout>

(6)

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.activityresulttest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.activityresulttest.MainActivity" 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="SubActivity"></activity> </application> </manifest>

参照

関連したドキュメント

昭33.6.14 )。.

製品開発者は、 JPCERT/CC から脆弱性関連情報を受け取ったら、ソフトウエア 製品への影響を調査し、脆弱性検証を行い、その結果を

QRコード読込画面 が表示されたら、表 示された画面を選択 してウインドウをアク ティブな状態にした 上で、QRコードリー

今回の SSLRT において、1 日目の授業を受けた受講者が日常生活でゲートキーパーの役割を実

ら。 自信がついたのと、新しい発見があった 空欄 あんまり… 近いから。

印刷物をみた。右側を開けるのか,左側を開け

 ファミリーホームとは家庭に問題がある子ど

②企業情報が「特定CO の発給申請者」欄に表示