android AlertDialog布局 ——2
16lz
2021-01-26
之前写了一篇android 关于AlertDialog 布局的博文,上次的布局是用setContentView()来进行布局,并强调了一些代码的编写顺序,今天我用setview对alertdialong进行布局。
代码如下:
Java代码
- /**
- *打开保存对话框框
- */
- privatevoidopenSaveDialog()
- {
- savePhotoDialog=newAlertDialog.Builder(SaveAndShareActivity.this);
- LayoutInflaterfactory=LayoutInflater.from(SaveAndShareActivity.this);
- Viewview=factory.inflate(R.layout.save_dialog_layout,null);
- editText=(EditText)view.findViewById(R.id.EditTextPhotoName);
- savePhotoDialog.setIcon(R.drawable.icon_save);
- savePhotoDialog.setTitle("保存图片");
- savePhotoDialog.setView(view);
- savePhotoDialog.setPositiveButton("确定",newDialogInterface.OnClickListener(){
- publicvoidonClick(DialogInterfacedialog,intwhichButton)
- {
- savePhotoName=editText.getText().toString();
- if(savePhotoName!=null)
- {
- Toast.makeText(SaveAndShareActivity.this,savePhotoName,Toast.LENGTH_SHORT)
- .show();
- savePhotoDialog.create().dismiss();
- }
- else
- {
- Toast.makeText(SaveAndShareActivity.this,"文件名不能为空!",Toast.LENGTH_SHORT)
- .show();
- }
- }
- });
- savePhotoDialog.setNegativeButton("取消",newDialogInterface.OnClickListener(){
- publicvoidonClick(DialogInterfacedialog,intwhichButton)
- {
- savePhotoDialog.create().dismiss();
- }
- });
- savePhotoDialog.create().show();
- }
布局文件:
Java代码
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="260dip"
- android:layout_height="200dip"
- android:gravity="center"
- android:orientation="vertical">
- <TextView
- android:id="@+id/TextViewPhotoName"
- android:layout_width="200dip"
- android:layout_height="wrap_content"
- android:text="@string/save_photo_name"
- android:textColor="#fff"
- android:textSize="20sp"/>
- <EditText
- android:id="@+id/EditTextPhotoName"
- android:layout_width="200dip"
- android:layout_height="wrap_content"
- android:text="@string/default_photo_name"/>
- </LinearLayout>
这些代码摘自我项目的一部分,大家捡一些有用的信息,ok!
注:个人见解,可提意见或建议,勿扔板砖,谢谢!
更多相关文章
- Android绘制进阶之四:在位图上绘制文本并旋转
- Android侧拉框Demo
- Android(安卓)4.1/4.1.1 TextView.setText for Html 引发ArrayIn
- H5 调用android原生相机代码分析
- android 逆向工程-工具篇 jadx(九)
- 菜鸟Android(1)
- 浅入浅出Android(007):看看你的手机上有哪些传感器
- android的Fragment解析(一行代码引发的思考)
- Android(安卓)CoordinatorLayout高级用法之自定义Behavior