歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android下按鈕的使用方法

Android下按鈕的使用方法

日期:2017/3/1 10:46:36   编辑:Linux編程

Android下按鈕的使用方法:

  1. package com.hangsheng.button;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. public class Ex07_WidgetButtonActivity extends Activity {
  7. /** Called when the activity is first created. */
  8. @Override
  9. public void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.main); //關聯布局模板文件main.xml
  12. find_and_modify_button(); //調用成員函數find_and_modity_button
  13. }
  14. private void find_and_modify_button(){
  15. Button button =(Button)findViewById(R.id.button); //通過資源內ID為button的資源來實例化button對象
  16. button.setOnClickListener(button_listener); //設置對象button的監聽器
  17. };
  18. private Button.OnClickListener button_listener =new Button.OnClickListener(){ //成員按鈕監聽對象
  19. @Override
  20. public void onClick(View v) { //按鈕事件
  21. // TODO Auto-generated method stub
  22. setTitle("button被點擊了一下");
  23. }
  24. };
  25. }

布局模板文件main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/hello" />
  10. <Button
  11. android:id="@+id/button"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="這是Button" />
  15. </LinearLayout>
Copyright © Linux教程網 All Rights Reserved