ボタンを押してみる
Eclipse3.4からEclipse3.5へ - 会者定離で以降でのkyoto-gtugの資料を参考にHelloAndoridってボタン押してテキスト変更するアプリを試してたけど、どうも上手くいかない。
で別資料を物色。以下を発見、一先ずコピー。で動作確認。
勉強会/ボタンをつくってみよう - 日本Androidの会(日本アンドロイドの会)
動いているので、これをちょっといじって、TextView変えれるようにする。
って、変えれたけど、最初に作っていたのと同じような?まあいいか、気にしない。
package jp.android.helloandroid; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class HelloAndroid extends Activity implements OnClickListener{ //<=3、Lisenerを実装 /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b = (Button)findViewById(R.id.Button01); //<=1、ボタン取り出して b.setOnClickListener(this); //<=2、Listenerを追加して(Listener自体は未実装) } public void onClick(View v) { //<=4、関数雛形をEclipseが補完してくれるので、あとは内部実装。 Button b = (Button)v; b.setText("Button01_pushed"); TextView v1 = (TextView)findViewById(R.id.Text01); v1.setText("Text has changed."); } }
で、ボタンを離したらことを通知するListenerは、どこだ?