新聞中心
Android自帶的 AlertDialog,ProgressDialog,DatePickerDialog,TimePickerDialog 可以用于簡單的對話框顯示。當還是有這些對話框不能滿足應(yīng)用需要的時候,這時就可以使用一些自定義的對話框。有多種方法可以實現(xiàn)自定義對話框。一是使用 Activity作為Dialog,可以通過設(shè)置Activity顯示Dialog風格,使得該Activity在外觀上和Dialog一致:顯示在其它 Activity前面且半透明。

10年積累的成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有鎮(zhèn)賚免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
本例采用另外兩種方法來使用自定義對話框,將用這個對話框來最為圖形變換(TRANSFORM)的選項:
Primitive: Rectange, Ellipse,Text
Pen: Thin,Thick,Dashed
Brush: Solid, Gradient,Texture
Transform: Identity, Rotate, Scale, Shear
Rendering: Stroke, Fill, Stoke and Fill
首先在res\layout 下新建transformoption.xml 作為自定義對話框布局:
- xmlns:android=”http://schemas.android.com/apk/res/android”
- android:layout_width=”fill_parent”
- android:layout_height=”fill_parent”
- >
- android:layout_width=”fill_parent”
- android:layout_height=”fill_parent”>
- android:layout_width=”fill_parent”
- android:layout_height=”fill_parent”
- android:orientation=”vertical”
- >
- android:text=”Primitive”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Rectangle”
- android:id=”@+id/radioRectangle”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Ellipse”
- android:id=”@+id/radioEllipse”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Text”
- android:id=”@+id/radioText”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Pen”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Thin”
- android:id=”@+id/radioThin”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Thick”
- android:id=”@+id/radioThick”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Dashed”
- android:id=”@+id/radioDashed”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Brush”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Solid”
- android:id=”@+id/radioSolid”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Gradient”
- android:id=”@+id/radioGradient”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Texture”
- android:id=”@+id/radioTexture”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Transform”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Identity”
- android:id=”@+id/radioIdentity”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Rotate”
- android:id=”@+id/radioRotate”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Scale”
- android:id=”@+id/radioScale”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Shear”
- android:id=”@+id/radioShear”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Rendering”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Stroke”
- android:id=”@+id/radioStroke”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Fill”
- android:id=”@+id/radioFill”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
- android:text=”Stroke and Fill”
- android:id=”@+id/radioStrokeFill”
- android:layout_width=”wrap_content”
- android:layout_height=”wrap_content”>
一種方法是重新定制AlertDialog ,基本步驟和Android簡明開發(fā)教程十七:Dialog 顯示圖像類似,但是在protected Dialog onCreateDialog(int id) ,需要重新設(shè)定Dialog的Content View并給RadioButton添加事件處理:
- 1 protected Dialog onCreateDialog(int id) {
- 2 final Dialog dialog;
- 3 switch (id) {
- 4 case OPTION_DIALOG:
- 5 LayoutInflater li
- 6 = LayoutInflater.from(this);
- 7 View optionView
- 8 = li.inflate(R.layout.transformoption, null);
- 9 AlertDialog.Builder optionDialog
- 10 = new AlertDialog.Builder(this);
- 11 optionDialog.setTitle("Options");
- 12 optionDialog.setView(optionView);
- 13 dialog = optionDialog.create();
- 14 RadioButton button = (RadioButton) optionView
- 15 .findViewById(R.id.radioRectangle);
- 16 button.setOnClickListener(new Button.OnClickListener() {
- 17
- 18 public void onClick(View v) {
- 19 primitiveIndex = PRIMITIVE_RECTANGLE;
- 20 drawImage();
- 21 dialog.dismiss();
- 22
- 23 }
- 24 });
- 25
- 26 ...
- 27
- 28 button = (RadioButton) optionView
- 29 .findViewById(R.id.radioStrokeFill);
- 30 button.setOnClickListener(new Button.OnClickListener() {
- 31
- 32 public void onClick(View v) {
- 33 renderingIndex = RENDERING_STROKE_AND_FILL;
- 34 drawImage();
- 35 dialog.dismiss();
- 36
- 37 }
- 38 });
- 39 return dialog;
- 40 }
- 41
- 42 return null;
- 43
- 44 }
第二種是通過派生Dialog ,定義了一個OptionDialog類作為Dialog子類。
- 1 class OptionDialog extends Dialog {
- 2
- 3 public OptionDialog(Context context) {
- 4 super(context);
- 5 // TODO Auto-generated constructor stub
- 6 }
- 7
- 8 protected void onCreate(Bundle savedInstanceState) {
- 9 super.onCreate(savedInstanceState);
- 10 setContentView(R.layout.transformoption);
- 11 setTitle("Options");
- 12 RadioButton button
- 13 = (RadioButton) findViewById(R.id.radioRectangle);
- 14 button.setOnClickListener(new Button.OnClickListener() {
- 15
- 16 public void onClick(View v) {
- 17 primitiveIndex = PRIMITIVE_RECTANGLE;
- 18 drawImage();
- 19 dismiss();
- 20
- 21 }
- 22 });
- 23 ...
- 24 button = (RadioButton) findViewById(R.id.radioStrokeFill);
- 25 button.setOnClickListener(new Button.OnClickListener() {
- 26
- 27 public void onClick(View v) {
- 28 renderingIndex = RENDERING_STROKE_AND_FILL;
- 29 drawImage();
- 30 dismiss();
- 31
- 32 }
- 33 });
- 34 }
- 35
- 36 }
這兩種方法在顯示Dialog時有所不同:
- 1 private OptionDialog optionDialog;
- 2 static final private int OPTION_DIALOG = 1;
- 3
- 4 ...
- 5 optionDialog = new OptionDialog(this);
- 6
- 7 ...
- 8 @Override
- 9 public void onClick(View view) {
- 10 // optionDialog.show();
- 11 showDialog(OPTION_DIALOG);
- 12
- 13 }
下面是完整代碼:
- 1 public class Transform extends Graphics2DActivity
- 2 implements OnClickListener {
- 3
- 4 static int PRIMITIVE_RECTANGLE = 0;
- 5 static int PRIMITIVE_ELLIPSE = 1;
- 6 static int PRIMITIVE_TEXT = 2;
- 7 static int PEN_THIN = 0;
- 8 static int PEN_THICK = 1;
- 9 static int PEN_DASHED = 2;
- 10 static int BRUSH_SOLID = 0;
- 11 static int BRUSH_GRADIENT = 1;
- 12 static int BRUSH_TEXTURE = 2;
- 13 static int TRANSFORM_IDENTITY = 0;
- 14 static int TRANSFORM_ROTATE = 1;
- 15 static int TRANSFORM_SCALE = 2;
- 16 static int TRANSFORM_SHEAR = 3;
- 17 static int RENDERING_STROKE = 0;
- 18 static int RENDERING_FILL = 1;
- 19 static int RENDERING_STROKE_AND_FILL = 2;
- 20 int primitiveIndex;
- 21 int penIndex;
- 22 int brushIndex;
- 23 int transformIndex;
- 24 int renderingIndex;
- 25 int[] rgbData;
- 26 int bitmapWidth;
- 27 int bitmapHeight;
- 28
- 29 class OptionDialog extends Dialog {
- 30
- 31 public OptionDialog(Context context) {
- 32 super(context);
- 33 // TODO Auto-generated constructor stub
- 34 }
- 35
- 36 protected void onCreate(Bundle savedInstanceState) {
- 37 super.onCreate(savedInstanceState);
- 38 setContentView(R.layout.transformoption);
- 39 setTitle("Options");
- 40 RadioButton button
- 41 = (RadioButton) findViewById(R.id.radioRectangle);
- 42 button.setOnClickListener(new Button.OnClickListener() {
- 43
- 44 public void onClick(View v) {
- 45 primitiveIndex = PRIMITIVE_RECTANGLE;
- 46 drawImage();
- 47 dismiss();
- 48
- 49 }
- 50 });
- 51 button = (RadioButton) findViewById(R.id.radioEllipse);
- 52 button.setOnClickListener(new Button.OnClickListener() {
- 53
- 54 public void onClick(View v) {
- 55 primitiveIndex = PRIMITIVE_ELLIPSE;
- 56 drawImage();
- 57 dismiss();
- 58
- 59 }
- 60 });
- 61 button = (RadioButton) findViewById(R.id.radioText);
- 62 button.setOnClickListener(new Button.OnClickListener() {
- 63
- 64 public void onClick(View v) {
- 65 primitiveIndex = PRIMITIVE_TEXT;
- 66 drawImage();
- 67 dismiss();
- 68
- 69 }
- 70 });
- 71
- 72 button = (RadioButton) findViewById(R.id.radioThin);
- 73 button.setOnClickListener(new Button.OnClickListener() {
- 74
- 75 public void onClick(View v) {
- 76 penIndex = PEN_THIN;
- 77 drawImage();
- 78 dismiss();
- 79
- 80 }
- 81 });
- 82
- 83 button = (RadioButton) findViewById(R.id.radioThick);
- 84 button.setOnClickListener(new Button.OnClickListener() {
- 85
- 86 public void onClick(View v) {
- 87 penIndex = PEN_THICK;
- 88 drawImage();
- 89 dismiss();
- 90
- 91 }
- 92 });
- 93
- 94 button = (RadioButton) findViewById(R.id.radioDashed);
- 95 button.setOnClickListener(new Button.OnClickListener() {
- 96
- 97 public void onClick(View v) {
- 98 penIndex = PEN_DASHED;
- 99 drawImage();
- 100 dismiss();
- 101
- 102 }
- 103 });
- 104
- 105 button = (RadioButton) findViewById(R.id.radioSolid);
- 106 button.setOnClickListener(new Button.OnClickListener() {
- 107
- 108 public void onClick(View v) {
- 109 brushIndex = BRUSH_SOLID;
- 110 drawImage();
- 111 dismiss();
- 112
- 113 }
- 114 });
- 115
- 116 button = (RadioButton) findViewById(R.id.radioGradient);
- 117 button.setOnClickListener(new Button.OnClickListener() {
- 118
- 119 public void onClick(View v) {
- 120 brushIndex = BRUSH_GRADIENT;
- 121 drawImage();
- 122 dismiss();
- 123
- 124 }
- 125 });
- 126
- 127 button = (RadioButton) findViewById(R.id.radioTexture);
- 128 button.setOnClickListener(new Button.OnClickListener() {
- 129
- 130 public void onClick(View v) {
- 131 brushIndex = BRUSH_TEXTURE;
- 132 drawImage();
- 133 dismiss();
- 134
- 135 }
- 136 });
- 137
- 138 button = (RadioButton) findViewById(R.id.radioIdentity);
- 139 button.setOnClickListener(new Button.OnClickListener() {
- 140
- 141 public void onClick(View v) {
- 142 transformIndex = TRANSFORM_IDENTITY;
- 143 drawImage();
- 144 dismiss();
- 145
- 146 }
- 147 });
- 148
- 149 button = (RadioButton) findViewById(R.id.radioRotate);
- 150 button.setOnClickListener(new Button.OnClickListener() {
- 151
- 152 public void onClick(View v) {
- 153 transformIndex = TRANSFORM_ROTATE;
- 154 drawImage();
- 155 dismiss();
- 156
- 157 }
- 158 });
- 159
- 160 button = (RadioButton) findViewById(R.id.radioScale);
- 161 button.setOnClickListener(new Button.OnClickListener() {
- 162
- 163 public void onClick(View v) {
- 164 transformIndex = TRANSFORM_SCALE;
- 165 drawImage();
- 166 dismiss();
- 167
- 168 }
- 169 });
- 170
- 171 button = (RadioButton) findViewById(R.id.radioShear);
- 172 button.setOnClickListener(new Button.OnClickListener() {
- 173
- 174 public void onClick(View v) {
- 175 transformIndex = TRANSFORM_SHEAR;
- 176 drawImage();
- 177 dismiss();
- 178
- 179 }
- 180 });
- 181
- 182 button = (RadioButton) findViewById(R.id.radioStroke);
- 183 button.setOnClickListener(new Button.OnClickListener() {
- 184
- 185 public void onClick(View v) {
- 186 renderingIndex = RENDERING_STROKE;
- 187 drawImage();
- 188 dismiss();
- 189
- 190 }
- 191 });
- 192
- 193 button = (RadioButton) findViewById(R.id.radioFill);
- 194 button.setOnClickListener(new Button.OnClickListener() {
- 195
- 196 public void onClick(View v) {
- 197 renderingIndex = RENDERING_FILL;
- 198 drawImage();
- 199 dismiss();
- 200
- 201 }
- 202 });
- 203
- 204 button = (RadioButton) findViewById(R.id.radioStrokeFill);
- 205 button.setOnClickListener(new Button.OnClickListener() {
- 206
- 207 public void onClick(View v) {
- 208 renderingIndex = RENDERING_STROKE_AND_FILL;
- 209 drawImage();
- 210 dismiss();
- 211
- 212 }
- 213 });
- 214 }
- 215
- 216 }
- 217
- 218 private OptionDialog optionDialog;
- 219 private Button btnOptions;
- 220
- 221 static final private int OPTION_DIALOG = 1;
- 222
- 223 private AffineTransform at = new AffineTransform();
- 224 private int w, h;
- 225 private IShape shapes[] = new IShape[3];
- 226
- 227 private boolean firstTime = true;
- 228 private FontEx font = FontEx.getSystemFont();
- 229
- 230 @Override
- 231 protected void drawImage() {
- 232 drawTransform();
- 233
- 234 }
- 235
- 236 protected Dialog onCreateDialog(int id) {
- 237 final Dialog dialog;
- 238 switch (id) {
- 239 case OPTION_DIALOG:
- 240 LayoutInflater li
- 241 = LayoutInflater.from(this);
- 242 View optionView
- 243 = li.inflate(R.layout.transformoption, null);
- 244 AlertDialog.Builder optionDialog
- 245
網(wǎng)頁名稱:Android開發(fā)速成簡潔教程十八:自定義對話框Transform
當前鏈接:http://www.dlmjj.cn/article/djodjhc.html


咨詢
建站咨詢
