三部分:
1)在MainActivity中
private RidingDialog ridingDialog;
ridingDialog = new RidingDialog(IndexActivity.this,R.style.new_dialog);
//第一步 new一个Dialog(context,style)外框样式
ridingDialog.setRidingTimeOnClickListtener(new RidingTimeOnClickListtener() {
@Override
public void RidingTimeOnClick(String time,String ridingTime) {
index_starttime.setText(ridingTime);//返回数据显示在TextView上
TIME = time; //乘车时段用0 1 2 3 4 传到接口
}
});
//第二步 设立接口的Listener
index_starttime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
ridingDialog.show();
}
});
//第三步 调用显示出Dialog
2) 外框样式 R.Style.new
<style name="new_dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<!-- 边框 -->
<item name="android:windowIsFloating">true</item>
<!-- 是否悬浮在activity上 -->
<item name="android:windowIsTranslucent">false</item>
<!-- 半透明 -->
<item name="android:windowNoTitle">true</item>
<!-- 无标题 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 背景透明 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 模糊 -->
<item name="android:backgroundDimAmount">0.5</item>
<!-- 灰度 -->
<item name="android:windowContentOverlay">@null</item>
<!-- 对话框是否有遮盖 -->
<item name="android:colorBackgroundCacheHint">@null</item>
</style>
3)自定义的Dialog extends Dialog
3.1)setContentView(R.layout.dialog_ridingtime) 设置Dialog内部布局 上面一步是设置Dialog的外框布局Style
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/ridingtime_shape"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="@string/ridingtime"
android:textColor="#ffffff"
android:textSize="18sp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioButton
android:id="@+id/ridingtime_all"
style="@style/ridingtime_radiobuttonStyle"
android:text="00:00 - 24:00 全天" />
<TextView style="@style/grayline_Style" />
<RadioButton
android:id="@+id/ridingtime_zerotosix"
style="@style/ridingtime_radiobuttonStyle"
android:text="00:00 - 06:00 早晨" />
<TextView style="@style/grayline_Style" />
<RadioButton
android:id="@+id/ridingtime_sixtotwelve"
style="@style/ridingtime_radiobuttonStyle"
android:text="06:00 - 12:00 上午" />
<TextView style="@style/grayline_Style" />
<RadioButton
android:id="@+id/ridingtime_twelvetoeighteenth"
style="@style/ridingtime_radiobuttonStyle"
android:text="12:00 - 18:00 下午" />
<TextView style="@style/grayline_Style" />
<RadioButton
android:id="@+id/ridingtime_eighteenthtotwentyfourth"
style="@style/ridingtime_radiobuttonStyle"
android:background="@drawable/ridingtimebottom_selector"
android:text="18:00 - 24:00 晚上" />
</RadioGroup>
</LinearLayout>
3.2) 如何设置回调接口
private RidingTimeOnClickListtener ridingTimeOnClickListtener;
/**
* 设置乘车时间监听对象
* @param ridingTimeOnClickListtener 回调接口对象
*/
public void setRidingTimeOnClickListtener(RidingTimeOnClickListtener ridingTimeOnClickListtener){
this.ridingTimeOnClickListtener = ridingTimeOnClickListtener;
}
/**
* 选择乘车时间的回调接口
*/
public interface RidingTimeOnClickListtener{
void RidingTimeOnClick(String TIME , String ridingTime);
}
//简而言之 也就是
private AAOnClickListener aaOnClickListener;
public void setAAOnClickListener(AAOnClcikListener aaOnClickListener){
this.aaOnClickListener=aaOnClickListener; }
public interface AAOnClickListener{
void AAOnClick(XXX xxx,XXX xxx);
}
//对应Activity中 setAAOnClickListener(new AAOnClickListener(){
// @Override
// public void AAOnClick(){...}
// });
总的代码:
package com.bestpay.trainticket.views;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import com.bestpay.trainticket.R;
/**
* @author apao
* @create 2014-5-20下午3:01:23
*/
public class RidingDialog extends Dialog implements OnClickListener{
/**
* 乘车时间监听对象
*/
private RidingTimeOnClickListtener ridingTimeOnClickListtener;
/**
* 00:00 - 24:00
*/
private RadioButton ridingtime_all;
/**
* 00:00 - 06:00
*/
private RadioButton ridingtime_zerotosix;
/**
* 06:00 - 12:00
*/
private RadioButton ridingtime_sixtotwelve;
/**
* 12:00 - 18:00
*/
private RadioButton ridingtime_twelvetoeighteenth;
/**
* 18:00 - 24:00
*/
private RadioButton ridingtime_eighteenthtotwentyfourth;
private Activity context;
public RidingDialog(Activity context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
this.context = context;
setContentView(R.layout.dialog_ridingtime);
setCanceledOnTouchOutside(true);//设置dialog以外能获得焦点
initWindow();
initView();
initOtherEvent();
}
private void initWindow() {
// TODO Auto-generated method stub
Window dialogWindow = getWindow();
WindowManager windowManager = context.getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams windowLP = dialogWindow.getAttributes();
dialogWindow.setGravity(Gravity.CENTER);
windowLP.width = (int)(display.getWidth() * 0.7);
// windowLP.height = (int)(display.getHeight() * 0.6);
windowLP.alpha = 1f;
dialogWindow.setAttributes(windowLP);
}
private void initView() {
// TODO Auto-generated method stub
ridingtime_all = (RadioButton)findViewById(R.id.ridingtime_all);
ridingtime_zerotosix = (RadioButton)findViewById(R.id.ridingtime_zerotosix);
ridingtime_sixtotwelve = (RadioButton)findViewById(R.id.ridingtime_sixtotwelve);
ridingtime_twelvetoeighteenth = (RadioButton)findViewById(R.id.ridingtime_twelvetoeighteenth);
ridingtime_eighteenthtotwentyfourth = (RadioButton)findViewById(R.id.ridingtime_eighteenthtotwentyfourth);
}
private void initOtherEvent() {
// TODO Auto-generated method stub
ridingtime_all.setOnClickListener(this);
ridingtime_zerotosix.setOnClickListener(this);
ridingtime_sixtotwelve.setOnClickListener(this);
ridingtime_twelvetoeighteenth.setOnClickListener(this);
ridingtime_eighteenthtotwentyfourth.setOnClickListener(this);
}
/**
* 设置乘车时间监听对象
* @param ridingTimeOnClickListtener 回调接口对象
*/
public void setRidingTimeOnClickListtener(RidingTimeOnClickListtener ridingTimeOnClickListtener){
this.ridingTimeOnClickListtener = ridingTimeOnClickListtener;
}
/**
* 选择乘车时间的回调接口
* @author apao
*
*/
public interface RidingTimeOnClickListtener{
void RidingTimeOnClick(String TIME , String ridingTime);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.ridingtime_all:
ridingTimeOnClickListtener.RidingTimeOnClick("0",ridingtime_all.getText().toString());
break;
case R.id.ridingtime_zerotosix:
ridingTimeOnClickListtener.RidingTimeOnClick("1",ridingtime_zerotosix.getText().toString());
break;
case R.id.ridingtime_sixtotwelve:
ridingTimeOnClickListtener.RidingTimeOnClick("2",ridingtime_sixtotwelve.getText().toString());
break;
case R.id.ridingtime_twelvetoeighteenth:
ridingTimeOnClickListtener.RidingTimeOnClick("3",ridingtime_twelvetoeighteenth.getText().toString());
break;
case R.id.ridingtime_eighteenthtotwentyfourth:
ridingTimeOnClickListtener.RidingTimeOnClick("4",ridingtime_eighteenthtotwentyfourth.getText().toString());
break;
default:
break;
}
this.dismiss();
}
}
截图: