Android ボタンの基本
Android Buttonはアプリケーション内でクリックできるボタンを表示するために使用される一般的なユーザーインターフェイスコントロールです。Android Buttonの基本的な使い方を以下に示します。
- XMLレイアウトファイルにButtonコントロールを追加します。
<Button
android:id="@+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮文本" />
- Javaコード内で[button]コントロールを見つけて、クリックイベントを設定:
Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 处理按钮点击事件的代码
}
});
- ボタンの外観は、背景色、文字色、フォントサイズなどを設定するボタンのプロパティでカスタマイズできます。
<Button
android:id="@+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/button_background"
android:textColor="@color/button_text_color"
android:textSize="16sp"
android:text="按钮文本" />
- ボタンのクリックイベントハンドラーのコード内にて、別のActivityへの遷移や特定の機能の実行など、相応のロジックを実行させることで実現できる
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到另一个Activity的代码
Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
startActivity(intent);
// 执行特定功能的代码
// ...
}
});
Android Buttonの基本的な使用方法で、実際のニーズに応じてさらにカスタマイズ、拡張できます。