【androidアプリ開発 No.08】

Pocket

前回はボタンのクリックイベントでダイアログを表示するように作りましたが、
今回は少しだけ変更し、テキストボックスに入力した値を表示するようにしたいと思います。

では、画面にテキストボックスを追加しましょう。

res/layout/mail.xml

<?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"
    android:weightSum="1">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:textColor="#FF0000" 
    android:text="@string/hello"
    />
<Button
    android:layout_width="match_parent"
    android:id="@+id/button1"
    android:text="ザ・ワールド"
    android:layout_height="wrap_content"
    />
<!-- ▼ 追加 =========================== ▼ -->
<EditText
    android:layout_width="match_parent"
    android:id="@+id/editText1"
    android:layout_height="wrap_content"
    android:inputType="textMultiLine">
    <requestFocus></requestFocus>
</EditText>
<!-- ▲ 追加 =========================== ▲ -->
</LinearLayout>

次は処理を追加しましょう。

src/android.helloworld/HelloworldActivity.Java

package android.helloworld;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.AlertDialog;
//▼ 追加 =========================== ▼
import android.widget.EditText;
//▲ 追加 =========================== ▲

public class HelloworldActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // ボタンの設定
        Button theWorldButton = (Button)findViewById(R.id.button1);
        
        // ダイアログの生成
        final AlertDialog.Builder alertDailog;
        alertDailog = new AlertDialog.Builder(this);
        alertDailog.setTitle("スタンド");
        alertDailog.setMessage("時よ止まれ");
         
        // ボタン押下時の処理
        theWorldButton.setOnClickListener(new Button.OnClickListener() {
        	
            public void onClick(View v) {
                // ▼ 追加 =========================== ▼
                // テキストボックスの値を設定
                EditText editText1=(EditText)findViewById(R.id.editText1);
                
                // ダイアログのメッセージ設定
                alertDailog.setMessage(editText1.getText().toString());
                // ▲ 追加 =========================== ▲
                // ダイアログの表示
                alertDailog.show();
            }
        });
    }
}

実行するとテキストボックスに入力した値がダイアログで表示されます。

ちょっとずつですが基本的な機能を使えるようになってきました。
次回からは目覚ましアプリを作っていこうと思います。



Post Footer automatically generated by Add Post Footer Plugin for wordpress.

びのっち

関東圏で活動しているとてもマイペースなSEです。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*