ImageButton 是就图片按纽,指定一个图片资源,就可以,其它的使用和 Button 是一样的,这里再熟悉一下对话框的使用,代码如下
xml 代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ImageButton
android:id="@+id/ib1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageButton
android:id="@+id/ib2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageButton
android:id="@+id/ib3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageButton
android:id="@+id/ib4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ImageButton
android:id="@+id/ib1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageButton
android:id="@+id/ib2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageButton
android:id="@+id/ib3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageButton
android:id="@+id/ib4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
java代码
package zziss.android.imagebuttontest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class ImageButtonTestActivity extends Activity implements android.view.View.OnClickListener
,android.content.DialogInterface.OnClickListener {
/** Called when the activity is first created. */
private ImageButton ib1 ;
private ImageButton ib2 ;
private ImageButton ib3 ;
private ImageButton ib4 ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ib1 = (ImageButton)this.findViewById(R.id.ib1);
ib2 = (ImageButton)this.findViewById(R.id.ib2);
ib3 = (ImageButton)this.findViewById(R.id.ib3);
ib4 = (ImageButton)this.findViewById(R.id.ib4);
ib1.setImageResource(R.drawable.phone);
ib2.setImageResource(R.drawable.tetris);
ib3.setImageResource(android.R.drawable.btn_star);
ib4.setImageResource(android.R.drawable.sym_action_call);
ib1.setOnClickListener(this);
ib2.setOnClickListener(this);
ib3.setOnClickListener(this);
ib4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageButton ib = (ImageButton)v;
AlertDialog dlg = new AlertDialog.Builder(this)
.setTitle("按纽点击")
.setMessage(Integer.toString(ib.getId()))
.setPositiveButton("确定",this )
.setNegativeButton("取消",this)
.create();
dlg.show();
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if (which == DialogInterface.BUTTON_POSITIVE)
{
Toast toast = Toast.makeText(this, "对话框", Toast.LENGTH_LONG);
toast.show();
}
else
{
Toast toast = Toast.makeText(this, "对话框222", Toast.LENGTH_LONG);
toast.show();
}
}
}
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class ImageButtonTestActivity extends Activity implements android.view.View.OnClickListener
,android.content.DialogInterface.OnClickListener {
/** Called when the activity is first created. */
private ImageButton ib1 ;
private ImageButton ib2 ;
private ImageButton ib3 ;
private ImageButton ib4 ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ib1 = (ImageButton)this.findViewById(R.id.ib1);
ib2 = (ImageButton)this.findViewById(R.id.ib2);
ib3 = (ImageButton)this.findViewById(R.id.ib3);
ib4 = (ImageButton)this.findViewById(R.id.ib4);
ib1.setImageResource(R.drawable.phone);
ib2.setImageResource(R.drawable.tetris);
ib3.setImageResource(android.R.drawable.btn_star);
ib4.setImageResource(android.R.drawable.sym_action_call);
ib1.setOnClickListener(this);
ib2.setOnClickListener(this);
ib3.setOnClickListener(this);
ib4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageButton ib = (ImageButton)v;
AlertDialog dlg = new AlertDialog.Builder(this)
.setTitle("按纽点击")
.setMessage(Integer.toString(ib.getId()))
.setPositiveButton("确定",this )
.setNegativeButton("取消",this)
.create();
dlg.show();
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if (which == DialogInterface.BUTTON_POSITIVE)
{
Toast toast = Toast.makeText(this, "对话框", Toast.LENGTH_LONG);
toast.show();
}
else
{
Toast toast = Toast.makeText(this, "对话框222", Toast.LENGTH_LONG);
toast.show();
}
}
}