MainActivity:
package com.sjm.dialog;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
@ViewInject(R.id.btn_dialog)
private Button btn_dialog;// 账号密码对话框
@ViewInject(R.id.tv_zuobiao)
private TextView tv_zuobiao;// 得到点击坐标
@ViewInject(R.id.tv_getTime)
private TextView tv_getTime;// 得到当前时间
@ViewInject(R.id.btn_timePickerDialog)
private Button btn_timePickerDialog;// 当前时间对话框
@ViewInject(R.id.btn_date)
private Button btn_date;// 点击得到当前日期
@ViewInject(R.id.tv_date)
private TextView tv_date;// 当前日期显示
@ViewInject(R.id.timePicker)
private TimePicker timePicker;// TimePicker组件
@ViewInject(R.id.btn_getTime)
private Button btn_getTime;// 得到TimePiceker组件的值
@ViewInject(R.id.datePicker)
private DatePicker datePicker;// DatePicker组件
@ViewInject(R.id.btn_getDate)
private Button btn_getDate;// 得到DatePicker组件的值
@ViewInject(R.id.scrollView)
private ScrollView scrollView;// 滚动组件
@ViewInject(R.id.linearLayout)
private LinearLayout linearLayout;// scrollView里面嵌套的组件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewUtils.inject(this);
btn_dialog.setOnClickListener(this);
btn_timePickerDialog.setOnClickListener(this);
btn_date.setOnClickListener(this);
btn_getTime.setOnClickListener(this);
btn_getDate.setOnClickListener(this);
timePicker.setIs24HourView(true);// 设置TimePicker组件是否是24小时制----------》如果不是24小时制会出现上午下午字样
// 重写onTouch()事件,在事件里通过requestDisallowInterceptTouchEvent(boolean)方法来设置父布局是否可用,true表示父布局不可用
// 解决地图的touch事件和scrollView的touch事件冲突问题
linearLayout.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
// 屏幕被抬起--->手指离开屏幕
if (event.getAction() == MotionEvent.ACTION_UP) {
// 允许ScrollView截断点击事件,ScrollView可滑动
scrollView.requestDisallowInterceptTouchEvent(false);
} else {
// 不允许ScrollView截断点击事件,点击事件由子View处理
scrollView.requestDisallowInterceptTouchEvent(true);
}
// 屏幕被按下------>手指按下
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// 允许ScrollView截断点击事件,ScrollView可滑动
scrollView.requestDisallowInterceptTouchEvent(false);
displayXY(event.getX(), event.getY());// 得到X坐标和Y坐标
} else {
// 不允许ScrollView截断点击事件,点击事件由子View处理
scrollView.requestDisallowInterceptTouchEvent(true);
}
// 在屏幕中拖动------>屏幕滑动
if (event.getAction() == MotionEvent.ACTION_MOVE) {
}
return false;
}
});
}
/**
* 在这里实现onTouchEvent--------------->当布局中有scrollView布局之后---->
* onTouchEvent事件不会被触发------->
* 可以使用scrollView的自布局的setOnTouchListener方法来监听是否触发滑动事件
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
System.out.println("111111111111111111");
// 在这里判断一下如果是按下操作就获取坐标然后执行方法
if (event.getAction() == MotionEvent.ACTION_DOWN) {
displayXY(event.getX(), event.getY());// 得到X坐标和Y坐标
}
return super.onTouchEvent(event);// 必须使用super.onTouchEvent(event)
}
/**
* 获取到坐标,进行判断
*
* @param x
* @param y
*/
private void displayXY(float x, float y) {
if (x > 80 && y > 300 && x < 200 && y < 600) {
tv_zuobiao.setText("点中了指定区域");
} else {
tv_zuobiao.setText("当前点击x坐标:" + x + "\n当前点击y坐标:" + y);
}
}
@SuppressLint("InflateParams")
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("登录");
// builder.setView(R.layout.login_dialog);//API最低21(5.0)新特性
final View view = getLayoutInflater().inflate(R.layout.login_dialog, null);
builder.setView(view);
// 右
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO 对接后台
EditText usernames = (EditText) view.findViewById(R.id.edit_username);
EditText password = (EditText) view.findViewById(R.id.edit_psd);
String username = usernames.getText().toString();
String psd = password.getText().toString();
if (TextUtils.isEmpty(username) & TextUtils.isEmpty(psd)) {
Toast toast = new Toast(getApplicationContext());
ImageView imageView = new ImageView(getApplicationContext());
imageView.setImageResource(R.drawable.ic_launcher);
TextView tv = new TextView(getApplicationContext());
tv.setText("fuck,你咋不输入账号密码");
tv.setTextColor(Color.BLUE);
tv.setTextSize(50);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(imageView);
layout.addView(tv);
toast.setView(layout);
toast.show();
} else {
Toast.makeText(getApplicationContext(),
"账号为:" + username.toString() + "\t密码为:" + psd.toString(), Toast.LENGTH_LONG).show();
}
dialog.dismiss();
}
});
// 左
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO 直接关闭
Toast.makeText(getApplicationContext(), "fuck", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
builder.setCancelable(false);
builder.show();
break;
case R.id.btn_timePickerDialog:// 弹出当前系统时间的对话框
TimePickerFragment dialog = new TimePickerFragment();
dialog.show(getFragmentManager(), "timepicker");
break;
case R.id.btn_date:// 弹出当前系统日期的对话框
DatePickerFragment datePickerFragment = new DatePickerFragment();
datePickerFragment.show(getFragmentManager(), "datePicker");
break;
case R.id.btn_getTime:// 得到TimePicker组件的值
getTime();
break;
case R.id.btn_getDate:
getDate();
break;
}
}
/**
* 获取DatePicker组件的值
*/
private void getDate() {
int year = datePicker.getYear();// 获取当前选中的年份
int month = datePicker.getMonth();// 获取当前选中的月份
int day = datePicker.getDayOfMonth();// 获取当前选中的月份的日期
btn_getDate.setText("当前选中的日期为:" + year + "年" + (month + 1) + "月" + day + "日");
}
/**
* 获取TimePicker组件的值
*/
private void getTime() {
int hour = timePicker.getCurrentHour();// 得到当前小时(选中的)
int minute = timePicker.getCurrentMinute();// 得到当前分钟(选中的)
btn_getTime.setText("当前选中的时间为:" + hour + ":" + minute);
}
/**
* 设置值-----》回调方法-----》显示时间到textview上
*
* @param hour
* @param minute
*/
public void setTimeValue(int hour, int minute) {
tv_getTime.setText(hour + ":" + minute);
}
/**
* DatePickerFragment回调
*
* @param year
* @param month
* @param day
*/
public void setDateValue(int year, int month, int day) {
tv_date.setText(year + "年" + (month + 1) + "月" + day + "日");
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
}
}
`
**DatePickerFragment:**
package com.sjm.dialog;
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.app.Dialog;
import android.app.DialogFragment;
import android.widget.DatePicker;
/**
* 当前日期对话框
*
* @author sjm
*
*/
public class DatePickerFragment extends DialogFragment implements OnDateSetListener {
private MainActivity mMainActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mMainActivity = (MainActivity) getActivity();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);// 获取到年份
int month = c.get(Calendar.MONTH);// 获取月份
int day = c.get(Calendar.DAY_OF_MONTH);// 获取当月天数
// 上下文,回调,初始年份,初始月份,初始天数
DatePickerDialog dailog = new DatePickerDialog(getActivity(), this, year, month, day);
return dailog;
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mMainActivity.setDateValue(year, monthOfYear, dayOfMonth);
}
}
“`
TimePickerFragment
package com.sjm.dialog;
import java.util.Calendar;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TimePicker;
/**
*
* 当前系统时间对话框
*
* @author sjm
*
*/
public class TimePickerFragment extends DialogFragment implements OnTimeSetListener {
private int hour;
private int minutes;
private int second;
private MainActivity mainActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 通过getActivity方法获得fragment所依赖的activity
mainActivity = (MainActivity) getActivity();// TODO:注意,只要是fragment都需要使用此方法来得到fragment所依赖的activity
}
/**
* 创建对话框事件,该方法会在MianActivity的按钮事件中调用show方法时,会检查是否已存在dialog,如果不存在时会出发此方法,
* 创建dialog,否则会直接显示
*/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// 获取当前的系统时间
Calendar c = Calendar.getInstance();// calendar日历
hour = c.get(Calendar.HOUR_OF_DAY);
minutes = c.get(Calendar.MINUTE);
// second = c.get(Calendar.SECOND);
// 创建时间对话框,参数为(上下文,设置时间事件,小时,分钟,是否为24小时制)
android.app.TimePickerDialog dialog = new android.app.TimePickerDialog(getActivity(), this, hour, minutes,
true);
return dialog;
}
/**
* 设置时间(实现OnTimeSetListener接口需要实现的方法)-------》时间对话框中的"完成"所对应的单击事件
*/
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO
this.hour = hourOfDay;
this.minutes = minute;
mainActivity.setTimeValue(hour, minutes);
}
}
[demo下载:]http://download.csdn.net/detail/qq_34411756/9617843