[PR] この広告は3ヶ月以上更新がないため表示されています。
ホームページを更新後24時間以内に表示されなくなります。

Androidプログラミング日記 (仮)  

ボタン作ってみる

あなたは

人目のプログラマーだよ。

Androidプログラミング日記 (仮).

 

share
 

 

 

ボタン

「画像表示プログラム 」の最後で言いましたが「Androidプログラムの仕組み」は見ましたでしょうか。ここまで来るとちょっと理解できるかもしれません。「Androidプログラムの仕組み」のことが出たのでその流れでレイアウト関係のボタンを配置して何かを表示というのをやってみます。

まずは

新しいプロジェクトを作りました。名前を「Botan」にしてファイルを開くと「BotanActivity.java」ができているので開きます。ついでに「layout」の「main.xml」と「values」の「String.xml」も開きましょう。

 

ファイル「main.xml」

01                    <?xml version="1.0" encoding="utf-8"?>
02<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03 android:layout_width="fill_parent"
04 android:layout_height="fill_parent"
05 android:orientation="vertical" >
06 
07  <TextView
08 android:layout_width="fill_parent"
09 android:layout_height="wrap_content"
10 android:text="@string/hello" />
11 
12</LinearLayout>
 

ファイル「String.xml」

1                    <?xml version="1.0" encoding="utf-8"?>
2<resources>
3 
4  <string name="hello">Hello World, BotanActivity!
5  <string name="app_name">Botan
6 
7</resources>

これを変更追加

 

ファイル「main.xml」

01                    <?xml version="1.0" encoding="utf-8"?>
02<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03  android:layout_width="fill_parent"
04  android:layout_height="fill_parent"
05  android:orientation="vertical" >
06 
07   <TextView
08    android:id="@+id/TextView01"
09    android:layout_width="fill_parent"
10    android:layout_height="wrap_content"
11    android:text="@string/hello"
12    android:textSize="12pt"
13  />
14   <Button
15    android:id="@+id/Button01"
16    android:layout_width="fill_parent"
17    android:layout_height="wrap_content"
18    android:text="@string/Button01"
19 
20  />
21 
22</LinearLayout>
 

ファイル「String.xml」

1                    <?xml version="1.0" encoding="utf-8"?>
2<resources>
3 
4   <string name="hello">ボタンを押してね。
5   <string name="Button01">クリック
6   <string name="app_name">Botan
7 
8</resources>

 

 

ファイル「BotanActivity.java」

01package and.roid;
02 
03import android.app.Activity;
04import android.os.Bundle;
05 
06public class BotanActivity extends Activity {
07  @Override
08  public void onCreate(Bundle savedInstanceState) {
09    super.onCreate(savedInstanceState);
10    setContentView(R.layout.main);
11  }
12}
 

ファイル「BotanActivity.java」

01                    package and.roid;
02 
03import android.app.Activity;
04import android.os.Bundle;
05import android.view.*;
06import android.view.View.OnClickListener;
07import android.widget.*;
08 
09public class BotanActivity extends Activity {
10  private TextView text1;
11  @Override
12  public void onCreate(Bundle savedInstanceState) {
13  super.onCreate(savedInstanceState);
14  setContentView(R.layout.main);
15 
16  text1 = (TextView)this.findViewById(R.id.TextView01);
17  Button btn1 = (Button)this.findViewById(R.id.Button01);
18  btn1.setOnClickListener(new MyClickAdapter());
19  }
20 
21  class MyClickAdapter implements OnClickListener {
22 
23    public void onClick(View view){
24     text1.setText("クリックしました!");
25    }
26  }
27}

 

text1 = (TextView)this.findViewById(R.id.TextView01);

こういう難しいコードが出たときは日本語で考えてみるのがいいですね。

「テキスト設定した変数text1 に main.xmlで設定しているidTextView01を取り出す」

という感じでしょうか。

Button btn1 = (Button)this.findViewById(R.id.Button01);

も同じですね。「ボタン設定した・・」になるだけですね。main.xmlで設定しているid○○を取り出すていうのがfindViewByIdというコードぽいですね。

実行の構成を変えて実行してみると

 

「クリック」と書かれたボタンを押すと上記のテキストがかわりました。

イベントリスナー

ボタンを押したら○○する。とかクリックしたら○○とかタッチしたら○○もそうですが、ユーザーが何かした、こういうことを「イベント」といいます。このイベントをが発生したときにその処理を代行するのが「イベントリスナー」といいます。

今回はボタンをクリックした時のイベント処理をするのに「OnClickListener」というイベントリスナーを用意しまし。これを「OnClickListener」でButtonに組み込んでいます。「setOnClickListener」というメソッドで登録するのですが単独では使えないメソッドなので「MyClickAdapter」というクラスを作り、このクラスから呼び出すようにします。「OnClickListener」には「onClick」というメソッドが用意されていて、クリックイベントで呼び出されるメソッドで、ここに処理を書けばボタンをクリックしたときの動作ができるようになります。今回は最初にtext1にセットされたmain.xmlに登録されているid「TextView01」に入っている「ボタンを押してね。」を「クリックしました!」に変更しています。

おまけ

「BotanActivity.java」の「MyClickAdapter」クラスをちょっと改造

  class MyClickAdapter implements OnClickListener {

  public void onClick(View view){
  Toast toast = Toast.makeText(getApplicationContext(), "クリックしました!", Toast.LENGTH_LONG);
  toast.show();

  }
}

こちらのほうがいい感じですね。

レイアウトは、エディッターで部品をドラッグして配置するやり方もあるのですが、写真など撮るのがめんどくさかったのと、ついでにコードも勉強できるということもあったので、こういう形にしました。自分で何かアプリを作るようになったら色々試してみようと思います。

 

ちょっとづつ役に立つコードが出てきますね。がんばっていきましょう!

 

 

Androidプログラミング日記 (仮) | サイトマップ | 個人情報保護方針 | 応援メールテヘペロ | ©2012 Japan  相互リンク大募集中です