您的位置:首页 > 其它

自定义对话框

2016-07-18 16:36 246 查看
1.布局显示文件

<?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();}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: