Android Button Example | Examples Java Code Geeks
安卓的UI中最基本设计是按钮操作,该文图示了如何用Eclipse可视化给安卓Android项目增加按钮。
首先创建一个Android Activity:
完成后,在res/layout/main.xml文件被创建:
打开这个main.xml, Eclipse出现如下可视化界面:
将按钮从左边form widgets拖到右边界面中,main.xml出现了如下配置:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="www.javacodegeeks.com" />
然后,回到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 wantbutton = (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);
}
});
}
}
运行代码效果如下:
[该贴被admin于2012-12-12 15:50修改过]