关于对话框dialog的使用以及dialog的继承
?beerwine.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? ?android:orientation="vertical" android:stretchColumns="0"
? ? ?android:background="@drawable/bg" android:layout_gravity="center_vertical|center_horizontal"
? ? ?android:layout_height="wrap_content" android:layout_width="wrap_content">
? ? ?<TableRow>
? ? ?? ? ?<TextView android:textColor="#ffffff" android:text="beer:"
? ? ?? ? ?? ? ?android:textSize="16px" android:layout_marginLeft="6dip"
? ? ?? ? ?? ? ?android:layout_gravity="center_vertical" />
? ? ?? ? ?<CheckBox android:id="@+id/checkboxBeer"
? ? ?? ? ?? ? ?android:layout_gravity="right|top" />
? ? ?</TableRow>
? ? ?<TableRow>
? ? ?? ? ?<TextView android:textColor="#ffffff" android:text="wine:"
? ? ?? ? ?? ? ?android:textSize="16px" android:layout_marginLeft="6dip"
? ? ?? ? ?? ? ?android:layout_gravity="center_vertical" />
? ? ?? ? ?<CheckBox android:id="@+id/checkboxWine"
? ? ?? ? ?? ? ?android:layout_gravity="right|top" />
? ? ?</TableRow>
? ? ?<TableRow android:layout_marginTop="230px">
? ? ?? ? ?<Button android:id="@+id/buttonOK" android:text="ok"
? ? ?? ? ?? ? ?android:layout_height="wrap_content" android:layout_width="wrap_content" />
? ? ?</TableRow>
</TableLayout>
public class Second extends Activity {
? ? ?@Override
? ? ?public void onCreate(Bundle savedInstanceState) {
? ? ?? ? ?super.onCreate(savedInstanceState);
? ? ?? ? ?Dialog dialog = new Dialog(this);
? ? ?? ? ?dialog.setContentView(R.layout.beerwine);
? ? ?? ? ?dialog.setTitle("beer or wine");
? ? ?? ? ?dialog.show();
? ? ?}
}
上面的代码实现了那张图片的功能,其中图片中的酒杯是作为背景出现的并不存在一个vimageView
主要?dialog.setContentView(R.layout.beerwine); 的使用。
?
如果继续使用点击事件
public class Second extends Activity {
? ? ?@Override
? ? ?public void onCreate(Bundle savedInstanceState) {
? ? ?? ? ?super.onCreate(savedInstanceState);
? ? ?? ? ?Dialog dialog = new Dialog(this);
? ? ?? ? ?dialog.setContentView(R.layout.beerwine);
? ? ?? ? ?dialog.setTitle("beer or wine");
? ? ?? ? ?dialog.show();
? ? ?? ? ?Button buttonOK = (Button) dialog.findViewById(R.id.buttonOK);
? ? ?? ? ?buttonOK.setOnClickListener(new OKListener(dialog));
? ? ?}
? ? ?protected class OKListener implements OnClickListener {
? ? ?? ? ?private Dialog dialog;
? ? ?? ? ?public OKListener(Dialog dialog) {
? ? ?? ? ?? ? ?this.dialog = dialog;
? ? ?? ? ?}
? ? ?? ? ?public void onClick(View v) {
? ? ?? ? ?? ? ?dialog.dismiss();
? ? ?? ? ?}
? ? ?}
}
这里面一定要注意Button buttonOK = (Button) dialog.findViewById(R.id.buttonOK);
是通过dialog引用出button其实你也可以用inflater的。
3.如果想继续扩展 添加选择框的选择事件
public class Third extends Activity {
? ? ?@Override
? ? ?public void onCreate(Bundle savedInstanceState) {
? ? ?? ? ?super.onCreate(savedInstanceState);
? ? ?? ? ?BeerWineDialog dialog = new BeerWineDialog(this, true, true);
? ? ?? ? ?dialog.show();
? ? ?}
}
?
public class BeerWineDialog extends Dialog {
? ? ?private boolean beer;
? ? ?private boolean wine;
? ? ?public BeerWineDialog(Context context, boolean beer, boolean wine) {
? ? ?? ? ?super(context);
? ? ?? ? ?this.beer = beer;
? ? ?? ? ?this.wine = wine;
? ? ?}
? ? ?@Override
? ? ?public void onCreate(Bundle savedInstanceState) {
? ? ?? ? ?super.onCreate(savedInstanceState);
? ? ?? ? ?setContentView(R.layout.beerwine);
? ? ?? ? ?setTitle("beer or wine");
? ? ?? ? ?CheckBox cbBeer = (CheckBox) findViewById(R.id.checkboxBeer);
? ? ?? ? ?cbBeer.setChecked(beer);
? ? ?? ? ?CheckBox cbWine = (CheckBox) findViewById(R.id.checkboxWine);
? ? ?? ? ?cbWine.setChecked(wine);
? ? ?? ? ?
? ? ?? ? ?Button buttonOK = (Button) findViewById(R.id.buttonOK);
? ? ?? ? ?buttonOK.setOnClickListener(new OKListener());
? ? ?}
? ? ?private class OKListener implements android.view.View.OnClickListener {
? ? ?? ? ?@Override
? ? ?? ? ?public void onClick(View v) {
? ? ?? ? ?? ? ?BeerWineDialog.this.dismiss();
? ? ?? ? ?}
? ? ?}
}
这也呢最终继承了一个dialog控件