Android自带的 AlertDialog,ProgressDialog,DatePickerDialog,TimePickerDialog 可以用于简单的对话框显示。当还是有这些对话框不能满足应用需要的时候,这时就可以使用一些自定义的对话框。有多种方法可以实现自定义对话框。一是使用 Activity作为Dialog,可以通过设置Activity显示Dialog风格,使得该Activity在外观上和Dialog一致:显示在其它 Activity前面且半透明。
10年积累的成都网站制作、网站建设、外贸网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有镇赉免费网站建设让你可以放心的选择与我们合作。
本例采用另外两种方法来使用自定义对话框,将用这个对话框来最为图形变换(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简明开发教程十七:Dialog 显示图像类似,但是在protected Dialog onCreateDialog(int id) ,需要重新设定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
分享题目:Android开发速成简洁教程十八:自定义对话框Transform
URL地址:http://www.mswzjz.cn/qtweb/news45/497745.html攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能