自定义AlertDialog
来源:互联网 发布:教育信息化软件 编辑:程序博客网 时间:2024/05/17 04:37
直接上代码
java
public class MyDialog implements View.OnClickListener{ private Dialog dialog; private Context context; private Button queding; // 设置标题 private String title; // 设置content,为View类型 private View content; private MyDialogListener myDialogListener; public MyDialog(Context context,MyDialogListener myDialogListener) { this.context = context; this.myDialogListener = myDialogListener; } public void showDialog() { dialog = new Dialog(context, R.style.style); View view = LayoutInflater.from(context).inflate(R.layout.dialog, null); TextView textView = (TextView) view.findViewById(R.id.title); LinearLayout linear = (LinearLayout) view.findViewById(R.id.linear); queding = (Button) view.findViewById(R.id.queding); queding.setOnClickListener(this);// Title if (title == null) { textView.setVisibility(View.GONE); } else { textView.setText(title); }// content if (content == null) { linear.setVisibility(View.GONE); } else { linear.addView(content); } dialog.setContentView(view); dialog.show(); } public void setTitle(String string) { title = string; } public void setContent(View view) { this.content = view; } public void onClick(View v) { myDialogListener.onClick(v); } public interface MyDialogListener { public void onClick(View view); }}
style
<style name="style" parent="@android:style/Theme.Dialog"> <!--窗框--> <item name="android:windowFrame">@null</item> <!--窗口浮动--> <item name="android:windowIsFloating">true</item> <!--窗口半透明--> <item name="android:windowIsTranslucent">false</item> <!--窗口无标题--> <item name="android:windowNoTitle">true</item> <item name="android:background">#fff</item> <item name="android:windowBackground">@color/colorAccent</item> <!--背景模糊的功能--> <item name="android:backgroundDimEnabled">true</item> </style>
xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <TextView android:layout_width="260dp" android:layout_height="40dp" android:text="" android:id="@+id/title" android:gravity="center" /> <LinearLayout android:id="@+id/linear" android:layout_width="260dp" android:layout_height="wrap_content" android:orientation="vertical" /> <LinearLayout android:layout_width="260dp" android:layout_height="40dp" android:orientation="horizontal"> <Button android:layout_width="130dp" android:layout_height="40dp" android:text="取消" android:id="@+id/quxiao" /> <Button android:layout_width="130dp" android:layout_height="40dp" android:text="确定" android:id="@+id/queding" /> </LinearLayout></LinearLayout>
主Activity
MyDialog myDialog = new MyDialog(this, new MyDialog.MyDialogListener() { public void onClick(View view) { Toast.makeText(MainActivity.this,"小明",Toast.LENGTH_SHORT).show(); } }); myDialog.setTitle("xiaoming"); View view = LayoutInflater.from(this).inflate(R.layout.dialog1,null); myDialog.setContent(view); myDialog.showDialog();
0 0
- 自定义 alertDialog
- 自定义AlertDialog
- AlertDialog自定义
- 自定义AlertDialog
- 自定义 AlertDialog
- 自定义AlertDialog
- 自定义Alertdialog
- 自定义AlertDialog
- 自定义AlertDialog
- 自定义AlertDialog
- 自定义AlertDialog
- 自定义AlertDialog
- 自定义AlertDialog
- 自定义AlertDialog
- 自定义AlertDialog
- AlertDialog 自定义
- 自定义AlertDialog
- 自定义alertDialog
- 4.2 C语言变量类型和范围
- I2c
- 取子串
- 糖果机器人_0006_day2_账号密码的保存_调用
- Laravel5文章编辑edit和update方法笔记
- 自定义AlertDialog
- thinkphp not found 不能运用urlrewrite
- Android蓝牙串口通信遇到的问题
- 跟我一起学Multiple View Geometry多视图几何(5)编程实践课
- Eclipse代码注释时@author默认值Admin的修改
- HTML5+规范:Zip(管理文件压缩和解压)
- Android Studio 中配置Annotationes框架
- Mapjoin执行流程
- Spring(一):Spring概念、基本例子入门