在网上找了很多关于dialog的自定义样式的问题,还有很多人写得比较复杂,需要改动style什么的,或者是自定义dialog搞得很复杂,我最后还是找到了方法来实现。
下面是我的dialog布局xml文件:
[mw_shl_code=java,true]<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:layout_margin="50dp">
<RelativeLayout
android:id="@+id/rl_dialog_content"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:background="@drawable/alertdialog_bg">
<TextView
android:id="@+id/dialog_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="现在就打电话给客服:arjinmc"
android:layout_marginTop="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:maxLines="5"
android:gravity="center"
/>
<LinearLayout
android:id="@+id/ll_buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_margin="30dp">
<Button
android:id="@+id/dialog_cancel"
android:text="@string/alert_cancel"
android:background="@drawable/btn_long_white"
style="@style/dialog_button"
android:layout_weight="1"
android:textColor="@color/tabs_font"
/>
<Button
android:id="@+id/dialog_ok"
android:text="@string/alert_ok"
android:background="@drawable/btn_long_red"
style="@style/dialog_button"
android:layout_weight="1"
android:layout_marginLeft="5dp"/>
</LinearLayout>
</RelativeLayout>
<ImageButton
android:id="@+id/dialog_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/alertdialog_close"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
/>
</RelativeLayout>
[/mw_shl_code]
在代码中需要这样写就可以了:
[mw_shl_code=java,true]//布局文件转换为view对象
LayoutInflater inflaterDl = LayoutInflater.from(this);
RelativeLayout layout = (RelativeLayout)inflaterDl.inflate(R.layout.layout_dialog, null );
//对话框
final Dialog dialog = new AlertDialog.Builder(SettingActivity.this).create();
dialog.show();
dialog.getWindow().setContentView(layout);
//取消按钮
Button btnCancel = (Button) layout.findViewById(R.id.dialog_cancel);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_SHORT).show();
}
});
//确定按钮
Button btnOK = (Button) layout.findViewById(R.id.dialog_ok);
btnOK.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show();
}
});
//关闭按钮
ImageButton btnClose = (ImageButton) layout.findViewById(R.id.dialog_close);
btnClose.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});[/mw_shl_code]
非常easy!自己动手吧。
自定义dialog的样式并比较日期
<style name="myDialogTheme" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
1、布局main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/btn"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="按下按钮试试"/>
</LinearLayout>
2、ly_dialogcontent
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="center"
android:background="#88E0EEEE">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="20sp"
android:text="查询"
android:gravity="center"
android:layout_gravity="center"
android:padding="10dp"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="开始时间:"/>
<EditText
android:id="@+id/beginTime"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="开始时间:"/>
<EditText
android:id="@+id/endTime"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/search"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:text="查询"/>
<Button
android:id="@+id/cancel"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:text="取消"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
3、Mainactivity
package com.ct.dialog;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private Button btnBn;
private Dialog dlg;
private LayoutInflater mInflater;
private Calendar calendar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnBn = (Button)findViewById(R.id.btn);
init();
btnBn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dlg.show();
}
});
}
private void init(){
calendar = Calendar.getInstance();
dlg = new Dialog(MainActivity.this, R.style.myDialogTheme);
mInflater = LayoutInflater.from(MainActivity.this);
dlg.setCancelable(true);
dlg.setCanceledOnTouchOutside(true);
View view = mInflater.inflate(R.layout.ly_dialogcontent, null);
final EditText begin = (EditText)view.findViewById(R.id.beginTime);
final EditText end = (EditText)view.findViewById(R.id.endTime);
Button sbtn = (Button)view.findViewById(R.id.search);
Button clebt = (Button)view.findViewById(R.id.cancel);
begin.setInputType(InputType.TYPE_NULL);
end.setInputType(InputType.TYPE_NULL);
dlg.setContentView(view);
//取消
clebt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dlg.dismiss();
}
});
//查询
sbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String beginTime = begin.getText().toString();
String endTime = end.getText().toString();
if (beginTime.equals("")|| endTime.equals("")) {
Toast.makeText(MainActivity.this, "都不能为空", Toast.LENGTH_LONG).show();
}else if(!isLarge(beginTime, endTime)){
Toast.makeText(MainActivity.this, "结束时间不能比开始时间小", Toast.LENGTH_LONG).show();
} else {
dlg.dismiss();}
}
});
//开始时间
begin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final String second = "00";
DatePickerDialog dpg = new DatePickerDialog(MainActivity.this,
new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
begin.setText(year+"-"+
format(++monthOfYear)+"-"+
format(dayOfMonth));
TimePickerDialog tpg = new TimePickerDialog(MainActivity.this,
new OnTimeSetListener(){
@Override
public void onTimeSet(
TimePicker arg0, int hourOfDay,
int minute) {
// TODO Auto-generated method stub
begin.setText(begin.getText().toString()
+ " "
+ format(hourOfDay)
+ ":"
+ format(minute)
+ ":" + second);
}},calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE), true);
tpg.show();
}
}, calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
dpg.show();
}
});
//结束时间
end.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final String second = "00";
DatePickerDialog dpg = new DatePickerDialog(MainActivity.this,
new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
end.setText(year+"-"+
format(++monthOfYear)+"-"+
format(dayOfMonth));
TimePickerDialog tpg = new TimePickerDialog(MainActivity.this,
new OnTimeSetListener(){
@Override
public void onTimeSet(
TimePicker arg0, int hourOfDay,
int minute) {
// TODO Auto-generated method stub
end.setText(begin.getText().toString()
+ " "
+ format(hourOfDay)
+ ":"
+ format(minute)
+ ":" + second);
}},calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE), true);
tpg.show();
}
}, calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
dpg.show();
}
});
}
/**
* 比较两个时间的大小
* @throws ParseException
*
* */
public static boolean isLarge(String beginTime,String endTime) {
boolean flag = false;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date begin = sdf.parse(beginTime);
Date edn = sdf.parse(endTime);
if(edn.getTime() - begin.getTime() >0){
flag = true;
}else {
flag = false;
}
} catch (ParseException e) {
// TODO: handle exception
e.printStackTrace();
}
return flag;
}
/**
* 在一位数字前加0变成两位数字
*
* @param value
* @return
*/
private String format(int value) {
String s = String.valueOf(value);
if (s.length() == 1) {
s = "0" + s;
}
return s;
}
}
(在F:\java\z自定义dialog+点击弹出时间调整那个\MyDialogTheme)