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

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

テキストボック的なやつ

あなたは

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

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

 

share
 

 

 

テキストBOXみたいなやつ

今回はテキストBOXみたいなやつをやっていきます。Androidでタッチすると入力キーボードがでてくるようなやつですね。「エディトテキスト」というらしいです。

そろそろなんのアプリ作ろうかなとか考えちゃってるわたしがいます。ゲームはもちろん作りたいですよね。でも電話帳とかメーラーとかホーム画面的なやつとかってやっぱし難しいのでしょうか・・作りたいけど。今の知識じゃ絶対無理なことだけはわかってます!

エディトテキスト

今回も書籍から。前回の「ボタン作ってみる」を改造して作っていきます。テキストをユーザー入力してもらい、それをもとに計算をするプログラムをしていきます。だそうです。「ボタン作ってみる」のファイルを開いて下さい。そして

 

ファイル名「main.xml」

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

ファイル名「main.xml」

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

このファイルでの順番にレイアウトされるので、テキスト>エディトテキスト>ボタン の順番にします。

<EditText/>を付け加えました。内容も上から呼び出すためのID、大きさ、幅、高さでしょうか。ちなみに「fill_parent」は画面いっぱい「wrap_content」は定数らしいです。

 

 

ファイル「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>
 
  前回のこれを  
 

ファイル名「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;
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}
 
  前回のこれを  
 

ファイル名「BotanActivity.java」

01package and.roid;
02 
03import android.app.Activity;
04import android.os.Bundle;
05import android.text.Editable;
06import android.view.*;
07import android.view.View.OnClickListener;
08import android.widget.*;
09 
10public class BotanActivity extends Activity {
11private EditText edit1;
12 
13  @Override
14  public void onCreate(Bundle savedInstanceState) {
15  super.onCreate(savedInstanceState);
16  setContentView(R.layout.main);
17 
18  edit1 = (EditText)this.findViewById(R.id.EditText01);
19  Button btn1 = (Button)this.findViewById(R.id.Button01);
20  btn1.setOnClickListener(new MyClickAdapter());
21  }
22 
23  class MyClickAdapter implements OnClickListener {
24 
25  public void onClick(View view){
26   Editable e = edit1.getText();
27   int n = Integer.parseInt(e.toString());
28   int total = 0;
29   for(int i = n;i > 0;i--){
30   total += i;
31   }
32   Toast toast = Toast.makeText(getApplicationContext(),
33    "合計:"+total, Toast.LENGTH_LONG);
34   toast.show();
35   }
36  }
37}

private EditText edit1;

edit1 = (EditText)this.findViewById(R.id.EditText01);

テキスト「数字を入力してください」は変更とかはなく設定する必要はないので消して、エディトテキストを配置します。

Editable e = edit1.getText();

新しいメソッドが出てきました。よくわかりませんが、、getText();のゲットから、エディットテキストからテキストを取り出す(ゲットする)というこでしょう。

int n = Integer.parseInt(e.toString());

変数nに何か入れています。e.toString()からエディトテキストから取り出したテキストを、 Integerはテキストの前にきてカッコを付けると、どの言語でもだいたい数値にするというのが多いので、数値に変換するということのはず。

int total = 0;

変数totalを0にして設定。

for(int i = n;i > 0;i--){
   total += i;
   }

有名なコードですね。設定された数字分for{}で囲まれた中身を繰り返すというものです。total += i;は total = total + i ということの略です。

ということなので、テキストで取り出した回数n回分(int i = n)をマイナスしながら(i-- 10>9>8・・のように)値が0以上なら(i > 0){}内をtotal += iを繰り返すということになっています。つまり

2なら1+2、5なら1+2+3+4+5、10なら1+2+3+・・9+10のような計算をするということですね。

 

  こんな感じになりました。

いかがだったでしょうか。もう少しで基礎が終わります。そろそろ何を作りたいか心の中で叫んでおく必要がありますね。

では今回はこれで終わりです。

 

 

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