android--widget介绍之ImageButton
在UI设计中,Button是一个常用控件,但是Button太普通,不够艺术化,和页面的其他元素不协调,这时,往往会希望用图片代替,ImageButton由此产生。

如下是一个完整的demo,包含了click事件处理
activity代码:
说明: android有ImageButton控件,只需附上图片的来源即可。
在UI设计中,Button是一个常用控件,但是Button太普通,不够艺术化,和页面的其他元素不协调,这时,往往会希望用图片代替,ImageButton由此产生。

如下是一个完整的demo,包含了click事件处理
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
<Button android:text="ok1" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<ImageButton android:src="@drawable/lock1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/imageButton1"></ImageButton>
</LinearLayout>
<ImageButton android:src="@drawable/lock2" android:layout_height="wrap_content" android:layout_width="wrap_content"
android:id="@+id/imageButton2" android:layout_marginTop="30px"></ImageButton>
</LinearLayout>
activity代码:
public class Ex02Activity extends Activity {
private Button btnOk;
private ImageButton btnCc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnOk = (Button) findViewById(R.id.button1);
btnCc = (ImageButton) findViewById(R.id.imageButton1);
btnOk.setBackgroundColor(Color.BLUE);
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setTitle("this is btnOk");
}
});
btnCc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Ex02Activity.this, "BMI 計算器", Toast.LENGTH_SHORT).show();
/*new AlertDialog.Builder(Ex02Activity.this)
.setTitle("關於 Android BMI")
.setMessage("Android BMI Calc")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();*/
}
});
}说明: android有ImageButton控件,只需附上图片的来源即可。

在Android UI设计中,为了提升界面艺术感,通常会使用ImageButton替代普通的Button。本文通过一个包含点击事件处理的demo,介绍了如何在XML布局文件中设置ImageButton,并在活动代码中进行交互操作。
1628

被折叠的 条评论
为什么被折叠?



