Androidでは、通常のボタンを表示するには、 “http://developer.android.com/reference/android/widget/Button.html[android.widget.Button]”クラスを使用してください。
このチュートリアルでは、通常のボタンを表示する方法、クリックリスナを追加する方法、ユーザーがボタンをクリックしたとき、AndroidのインターネットブラウザでURLを開く方法について説明します。
P.SこのプロジェクトはEclipse 3.7で開発され、Android 2.3.3.
でテストされています
画像のようなもっと進歩した機能については、//android/android-imagebutton-example/[ImageButton example]とこのリンクを参照してください://android-android-imagebutton-selector-example/[ImageButtonセレクタの例]
1.ボタンを追加する
”
res/layout/main.xml
“ファイルを開き、ボタンを追加します。
File:res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout__width="fill__parent"
android:layout__height="fill__parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout__width="wrap__content"
android:layout__height="wrap__content"
android:text="Button - Go to mkyong.com"/>
</LinearLayout>
2.コードコード
クリックリスナーをボタンにアタッチします。
ユーザーがそれをクリックすると、モバイルブラウザを開き、URL:
/
を表示します。
ファイル:私のAndroid App Activity.java
package com.mkyong.android;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MyAndroidAppActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent browserIntent =
new Intent(Intent.ACTION__VIEW, Uri.parse("/"));
startActivity(browserIntent);
}
});
}
}
デモ
アプリケーションを実行します。
{空} 1。結果、通常のボタン。
{空} 2。ボタンをクリックし、ブラウザにURLを表示します。
ソースコードをダウンロードする
ダウンロードする –
Android-Button-Example.zip
(15 KB)
参考文献
ボタンJavaDoc]
リンク://タグ/アンドロイド/[アンドロイド]リンク://タグ/ボタン/[ボタン]