自定义对话框
2016-07-18 16:36
246 查看
1.布局显示文件
2.在Java代码中
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="280dp" android:layout_height="160dp" android:background="@drawable/custom_shape_dialog" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@+id/bottom" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="@color/black70" android:textSize="@dimen/textsize16" /> <TextView android:id="@+id/title_secondline" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="@color/black60" android:textSize="@dimen/textsize16" /> </LinearLayout> </RelativeLayout> <LinearLayout android:id="@+id/bottom" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center_horizontal" android:orientation="vertical" > <View android:layout_width="fill_parent" android:layout_height="1px" android:background="@color/black20" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal" > <Button android:id="@+id/cancle_btn" android:layout_width="fill_parent" android:layout_height="@dimen/line_height40" android:layout_centerHorizontal="true" android:textSize="@dimen/textsize16" android:textColor="@color/black70" android:layout_weight="1" android:background="@drawable/selector_common_cancel" /> <View android:layout_width="1px" android:layout_height="fill_parent" android:background="@color/black20" /> <Button android:id="@+id/submit_btn" android:layout_width="fill_parent" android:layout_height="@dimen/line_height40" android:layout_centerHorizontal="true" android:layout_marginLeft="1px" android:textSize="@dimen/textsize16" android:textColor="@color/main_green" android:layout_weight="1" android:background="@drawable/selector_common_confirm" /> </LinearLayout> </LinearLayout> </RelativeLayout>
2.在Java代码中
public class PublishDialog_L extends Dialog implements OnClickListener{ private OnDialogChooseLinstener onDialogChooseLinstener; private String title; private String title_secondline; private String lefttext; private String righttext; public PublishDialog_L(Context context, int theme, String title,String secondline,String left,String right, OnDialogChooseLinstener onDialogChooseLinstener) { super(context, theme); this.onDialogChooseLinstener = onDialogChooseLinstener; this.title = title; this.title_secondline = secondline; this.lefttext = left; this.righttext = right; } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.publish_dialog); getWindow().setWindowAnimations(R.style.confirm_dialog_animstyle); Button confirmBtn = (Button) findViewById(R.id.submit_btn); Button cancleBtn = (Button) findViewById(R.id.cancle_btn); TextView titleName = (TextView) findViewById(R.id.title); TextView titleName_secondline = (TextView) findViewById(R.id.title_secondline); titleName.setText(title); confirmBtn.setText(righttext); cancleBtn.setText(lefttext); if(title_secondline != null){ titleName_secondline.setText(title_secondline); titleName_secondline.setVisibility(View.VISIBLE); } confirmBtn.setOnClickListener(this); cancleBtn.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.submit_btn: dismiss(); break; case R.id.cancle_btn: onDialogChooseLinstener.onSubmit(""); dismiss(); break; } } }
OnDialogChooseLinstener是一个接口,里面有未实现方法,在实际调用上面这个接口的时候,实现里面的方法,根据选择做具体的操作
public interface OnDialogChooseLinstener { public void onSubmit(String strings); }
PublishDialog_L confirmDialog=new PublishDialog_L(context, R.style.Translucent_NoTitle, "确定你已经出师了?","你的学费将打给你的师傅","确定","取消", new OnDialogChooseLinstener() { @Override public void onSubmit(String strings) {
//在该处作具体的操作,strings既为从对话框的地方传过来的数据,(比如点击确定按钮的时候,携带了几个数据,把这些数据就放入了strings参数里面)
} }); confirmDialog.show();}
相关文章推荐
- 《韩非子》的法
- Java - 数据库(mysql - 3)
- 企业级云应用平台的实践和思考
- 基于静态数组的容器类bag实现
- Android选择图片(从相册或相机)并裁剪
- 挑战程序设计解题报告 2.6.2素数
- linux下找出文件中字符串出现次数的2种方法
- 关于字符驱动设备的一些概念理解
- 2016年找实习血泪史(上篇)
- 统计网站的每日访问量
- (IEEE-754) 字节数组与浮点数之间的互相转换(MODBUS float类型)
- <java编程思想>(thing in java) 阅读笔记(第十一章至第十二章)
- 大数据时代的技术hive:hive介绍
- 区块链技术原理
- leetcode_c++:链表:Palindrome Linked List(234)
- HDU - 2444 The Accomodation of Students(判断二分图 + 二分图匹配)
- 29. Divide Two Integers
- Java模拟斗地主发牌
- Ubuntu server下搭建Maven私服Nexus
- Fiddler界面详解