自定义preferences界面和对话框(带图标的preferences)

虽然市面上有很多天气预报,但是功能不喜欢

但是很想做个天气预报预警,想用Preferences做了个界面

界面如下

   效果图:

自定义preferences界面和对话框(带图标的preferences)_第1张图片


其中最主要的是重写 onCreateView方法

	@Override
	protected View onCreateView(ViewGroup parent) {
		// TODO Auto-generated method stub
		//如果不加这句,下拉的时候界面会被重新刷新,界面上的数据就会丢失
		if(view!=null)return view;

		view = LayoutInflater.from(getContext()).inflate(
				R.layout.perference_timerpicker, parent, false);
		icon = (ImageView) view.findViewById(R.id.image_icon);
		summary = (TextView) view.findViewById(android.R.id.summary);
		//android自定义的ID不能再开始时变化所以使用setSummary方法改变
	    this.setSummary(PreferenceSaveUtils.getString(this, getKey(), "08:00"));
//		summary.setText(PreferenceSaveUtils.getString(this, getKey(), "08:00"));
		return view;
	}

package com.cn.lxz.preferences;

import java.util.zip.Inflater;

import lxz.utils.preferences.PreferenceSaveUtils;

import android.app.TimePickerDialog;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.TimePicker;
import android.os.Parcelable;
import android.preference.DialogPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;

public class PreferenceSelectTimerDialog extends Preference implements
		OnPreferenceClickListener {

	private Context context;
	private View view;
	private ImageView icon;
	private TextView summary;

	public PreferenceSelectTimerDialog(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.setOnPreferenceClickListener(this);
		// this.setLayoutResource(R.layout.perference_timerpickerdialog);

	}

	@Override
	protected View onCreateView(ViewGroup parent) {
		// TODO Auto-generated method stub
		//如果不加这句,下拉的时候界面会被重新刷新,界面上的数据就会丢失
		if(view!=null)return view;

		view = LayoutInflater.from(getContext()).inflate(
				R.layout.perference_timerpicker, parent, false);
		icon = (ImageView) view.findViewById(R.id.image_icon);
		summary = (TextView) view.findViewById(android.R.id.summary);
		//android自定义的ID不能再开始时变化所以使用setSummary方法改变
	    this.setSummary(PreferenceSaveUtils.getString(this, getKey(), "08:00"));
//		summary.setText(PreferenceSaveUtils.getString(this, getKey(), "08:00"));
		return view;
	}


	@Override
	public boolean onPreferenceClick(Preference arg0) {
		// TODO Auto-generated method stub
		String time = PreferenceSaveUtils.getString(this, getKey(), "8:00");
		String[] mTime = time.split(":");
		Integer mHour = Integer.valueOf(mTime[0]);
		Integer mMinute = Integer.valueOf(mTime[1]);
		summary.setText((mHour > 9 ? "" + mHour : "0" + mHour) + ":"
				+ (mMinute > 9 ? "" + "" + mMinute : "0" + mMinute));

		new TimePickerDialog(this.context,
				new android.app.TimePickerDialog.OnTimeSetListener() {

					@Override
					public void onTimeSet(TimePicker view, int mHour,
							int mMinute) {
						// TODO Auto-generated method stub
						String time = (mHour > 9 ? "" + mHour : "0" + mHour)
								+ ":"
								+ (mMinute > 9 ? "" + "" + mMinute : "0"
										+ mMinute);
						PreferenceSaveUtils.putString(
								PreferenceSelectTimerDialog.this, getKey(),
								time);

						summary.setText(time);
					}
				}, mHour, mMinute, true).show();

		return false;
	}

}



代码地址:http://download.csdn.net/detail/b275518834/4674556


你可能感兴趣的:(String,null,Integer,Class,Constructor)