歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 應用開發筆記 - 按鈕(Button, ImageButton)

Android 應用開發筆記 - 按鈕(Button, ImageButton)

日期:2017/3/1 10:09:52   编辑:Linux編程
我們在講Toast時,已經使用了Button。首先,在面板拖拽圖標,然後更改相關屬性(Properties),如下圖:

activity_main.xml清單如下:

<Button

Android:id="@+id/btnOk"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/textView1"

android:layout_below="@+id/textView1"

android:layout_marginLeft="52dp"

android:layout_marginTop="76dp"

android:text="@string/btn1Caption"

android:textSize="@dimen/font_size20" />

然後,我們開始編碼。

其相關部分的代碼如下:

Button btnOk = (Button) findViewById(R.id.btnOk);

btnOk.setOnClickListener(this);

效果如下圖:


4.2.6 帶圖標的按鈕(ImageButton)

在面板拖拽圖標,然後更改相關屬性(Properties),如下圖:


activity_main.xml清單如下:

<ImageButton

android:id="@+id/imgBtn1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/chkBox2"

android:layout_alignTop="@+id/spin01"

android:contentDescription="@string/btn1Caption"

android:onClick="@string/hello_world"

android:src="@drawable/ic_launcher" />

然後,我們開始編碼。

其相關部分的代碼如下:

// ImageButton

ImageButton imgBtn1 = (ImageButton)findViewById(R.id.imgBtn1);

imgBtn1.setOnClickListener(newButton.OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generatedmethod stub

setToastString("Hello China").show();

}

});

效果如下圖:

Copyright © Linux教程網 All Rights Reserved