安卓Android 按鈕案例

banq發表於2012-12-11
Android Button Example | Examples Java Code Geeks

安卓的UI中最基本設計是按鈕操作,該文圖示瞭如何用Eclipse視覺化給安卓Android專案增加按鈕。

首先建立一個Android Activity:

安卓Android 按鈕案例

完成後,在res/layout/main.xml檔案被建立:

安卓Android 按鈕案例

開啟這個main.xml, Eclipse出現如下視覺化介面:

安卓Android 按鈕案例

將按鈕從左邊form widgets拖到右邊介面中,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="www.javacodegeeks.com" />

</LinearLayout>

然後,回到layout頁面,建立下面java檔案:

package com.javacodegeeks.android.buttonexample;

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 MainActivity extends Activity {

	private Button button;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		addListenerOnButton();

	}

	public void addListenerOnButton() {

        //Select a specific button to bundle it with the action you want
		button = (Button) findViewById(R.id.button1);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

			  Intent openBrowser =  new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.javacodegeeks.com"));
			  startActivity(openBrowser);
			}

		});

	}

}
<p class="indent">

執行程式碼效果如下:

安卓Android 按鈕案例

[該貼被admin於2012-12-12 15:50修改過]

相關文章