大家知道安卓按鈕Button技巧嗎?下面我們就給大家詳細介紹一下吧!
- public class ButtonActivity extends Activity {
-
- Context mContext = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- setContentView(R.layout.buttonview);
- mContext = this;
-
- //普通按鈕
- Button button0 = (Button)findViewById(R.id.buttonview0);
-
- //設置按鈕文字顏色
- button0.setTextColor(Color.BLUE);
- //設置按鈕文字大小
- button0.setTextSize(30);
-
- //設置按鈕監聽 點擊事件
- button0.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
- Toast.makeText(ButtonActivity.this, "您點擊了‘這是一個按鈕’", Toast.LENGTH_LONG).show();
-
- }
- });
-
- //帶圖片的按鈕
- ImageButton button1 = (ImageButton)findViewById(R.id.buttonview1);
- //設置按鈕監聽 點擊事件
- button1.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
- Toast.makeText(ButtonActivity.this, "您點擊了一個帶圖片的按鈕", Toast.LENGTH_LONG).show();
-
- }
- });
- super.onCreate(savedInstanceState);
- }
- }
復制代碼
- <?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:textColor="#000000"
- android:textSize="18dip"
- android:background="#00FF00"
- android:text="Button按鈕測試"
- android:gravity="center_vertical|center_horizontal"
- />
- <Button
- android:id="@+id/buttonview0"
- &nb