- The added line is THIS COLOR.
- The deleted line is THIS COLOR.
''[[MobileApp>Mobile Application 入門]]'' > ''[[Android>Android]]'' > ''[[GUI部品>Android GUI部品]]'' > ''ボタン''
~
*ボタンの表示 [#wd988f5b]
#ref(button.gif);
*ボタンの属性 [#md685236]
[[Buttonの属性>Android Buttonの属性]]
~
**ボタンを配置&クリック認識をさせる際のフォーマット (xml) [#td0adbb6]
**ボタンを配置&クリック認識をさせる際のフォーマット (Layoutのxml付きの場合) [#td0adbb6]
クリックはsetOnClickListenerでリスナーを登録した上で、~
onClick(View v)で ボタン押下時の処理を記述
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
import android.view.View.*;
public class TestTest extends Activity implements OnClickListener{
private Button button1;
private Button button2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.button_id);
// ボタンがクリックされた時に呼び出されるコールバック登録
button1.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button_id);
// ボタンがクリックされた時に呼び出されるコールバック登録
button2.setOnClickListener(this);
}
public void onClick(View v) {
// ボタンがクリックされた時に呼び出される
//Button button = (Button) v;
//ボタンを押した時の処理
if (v == button1){
処理1
}else if (v == button2){
処理2
}
}
}
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<!-- ボタンの定義。android:idを定義することでJavaから取り出せる -->
<Button
android:id="@+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_label"
/>
</LinearLayout>
~
**ボタンを配置するソースフォーマット (直書きの場合) [#td0adbb6]
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Test extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
Button button = new Button(this);
button.setText("button");
setContentView(button);
}
}
*参考 [#a3c90ec3]
[[日本Androidの会>http://android.siprop.org/index.php?%CA%D9%B6%AF%B2%F1%2F%A5%DC%A5%BF%A5%F3%A4%F2%A4%C4%A4%AF%A4%C3%A4%C6%A4%DF%A4%E8%A4%A6]]
~
~
''[[戻る>Android GUI部品]]''