PreferenceFragment DialogFragment Spinner NumberPicker

2024-08-23 11:58

本文主要是介绍PreferenceFragment DialogFragment Spinner NumberPicker,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

PreferencFragment解析:http://blog.csdn.net/lmj623565791/article/details/37970961/

billing_cycle.xml

<?xml version="1.0" encoding="UTF-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"android:title="@string/data_usage_summary_title"><Preferenceandroid:key="billing_cycle"android:title="@string/billing_cycle"/><Preference android:key="data_warning"android:title="@string/data_warning"/><SwitchPreferenceandroid:key="set_data_limit"android:title="@string/set_data_limit"/><Preferenceandroid:key="data_limit"android:title="@string/data_limit"/>
</PreferenceScreen>
data_usage_bytes_editor.xml
<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android:"http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:gravity="center"android:paddingStart="16dp"android:paddingEnd="16dp"android:layout_width="match_parent"android:layot_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android"><EditText android:id="@+id/bytes"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:minWidth="48dip"android:layout_gravity="center_vertical"android:focusable="true"android:focusableInTouchMode="true"android:inputType="numberDecimal"/><Spinner android:id="@+id/size_spinner"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:entries="@array/bytes_picker_sizes"/>
</LinearLayout>
data_usage_cycle_editor.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal" android:gravity="center"><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:textAppearance="?android:attr/textAppearanceMedium"android:text="@string/data_usage_cycle_editor_subtitle"/><NumberPicker android:id="@+id/cycle_day"android:layout_width="48dip"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_marginStart="16dip"android:layout_marginEnd="16dip"android:focusable="true"android:focusableInTouchMode="true"/></LinearLayout>
BillingCycleSettings.java(这里的DataUsageBase是PreferenceFragment的子类)
package com.example.util;import android.app.AlertDialog;
import android.net.NetworkTemplate;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.Preference.OnPreferenceChangeListener;
import android.support.v7.preference.Preference;
import android.support.v14.preference.SwitchPreference;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.Spinner;public class BillingCycleSettings extends DataUsageBase implementsPreference.OnPreferenceChangeListener{private static final String TAG="BillingCycleSettings";private static final boolean LOGO=true;private static final String TAG_CONFIRM_LIMIT="confirmLimit";private static final String TAG_CYCLE_EDITOR="cycleEditor";private static final String TAG_WARNING_EDITOR="warningEditor";private static final String KEY_BILLING_CYCLE="billing_cycle";private static final String KEY_DATA_WARNING="data_warning";private static final String KEY_SET_DATA_LIMIT="set_data_limit";private static final String KEY_DATA_LIMIT="data_limit";private NetworkTemplate mNetworkTemplate;private Preference mBillingCycle;private Preference mDataWarning;private android.preference.SwitchPreference mEnableDataLimit;private android.preference.Preference mDataLimit;private DataUsageController mDataUsageController;@Overridepublic void onCreate(Bundle icicle){super.onCreate(icicle);mDataUsageController=new DataUsageController(getContext());Bundle args=getArguments();mNetworkTemplate=args.getParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE);addPreferencesFromResource(R.xml.billing_cycle);mBillingCycle=findPreference(KEY_BILLING_CYCLE);mDataWarning=findPreference(KEY_DATA_WARNING);mEnableDataLimit=(SwitchPreference)findPreference(KEY_SET_DATA_LIMIT);mEnableDataLimit.setOnPreferenceChangeListener(this);mDataLimit=findPreference(KEY_DATA_LIMIT);}@Overridepublic void onResume(){super.onResume();updatePrefs();}private void updatePrefs(){NetworkPolicy policy=services.mPolicyEditor.getPolicy(mNetworkTemplate);mBillingCycle.setSummary(getString(R.string.billing_cycle_summary,policy!=null?policy.cycleDay:1));mDataWarning.setSummary(Formatter.formatFileSize(getContext(),policy!=null?policy.warningBytes:DataUsageController.DEFAULT_WARNING_LEVEL));if(policy!=null && policy.limitBytes!=LIMIT_DISABLED){mDataLimit.setSummary(Formatter.formatFileSize(getContext(),policy.limitBytes));mDataLimit.setEnabled(true);mEnableDataLimit.setChecked(true);}else{mDataLimit.setSummary(null);mDataLimit.setEnabled(false);mEnableDataLimit.setChecked(false);}}@Overridepublic boolean onPreferenceTreeClick(Preference preference){if(preference==mBillingCycle){CycleEditorFragment.show(this);return true;}else if(preference==mDataWarning){BytesEditorFragment.show(this,false);return true;}else if(preference==mDataLimit){BytesEditorFragment.show(this,true);return true;}return super.onPreferenceTreeClick(preference);}@Overridepublic boolean onPreferenceChange(Preference preference,Object newValue){if(mEnableDataLimit==preference){boolean enabled=(Boolean)newValue;if(enabled){ConfirmLimitFragment.show(this);}else{setPolicyLimitBytes(LIMIT_DISABLED);}return true;}return false;}@Overrideprotected int getMetricsCategory(){return MetricsEvent.BILLING_CYCLE;}private void setPolicyLimitBytes(long limitBytes){services.mPolicyEditor.setPolicyLimitBytes(mNetworkTemplate,limitBytes);updatePrefs();}public static class BytesEditorFragment extends DialogFragmentimplements DialogInterface.OnClickListener{private static final String EXTRA_TEMPLATE="template";private static final String EXTRA_LIMIT="limit";private View mView;public static void show(BillingCycleSettings parent,boolean isLimit){if(!parent.isAdded())return;final Bundle args=new Bundle();args.putParcelable(EXTRA_TEMPLATE, parent.mNetworkTemplate);args.putBoolean(EXTRA_LIMIT, isLimit);final BytesEditorFragment dialog=new BytesEditorFragment();dialog.setArguments(args);dialog.setTargetFragment(parent, 0);dialog.show(parent.getFragmentManager(),TAG_WARNING_EDITOR);}@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState){final Context context=getActivity();//Fragment里面的getActivity方法可以得到对应的Contextfinal LayoutInflater DialogInflater=LayoutInflater.from(context);final boolean isLimit=getArguments().getBoolean(EXTRA_LIMIT);mView=DialogInflater.inflate(R.id.data_usage_bytes_editor, null,false);setupPicker((EditText)mView.findViewById(R.id.bytes),(Spinner)mView.findViewById(R.id.size_spinner));return new AlertDialog.Builder(context).setTitle(isLimit?R.string.data_usage_limit_editor_title:R.string.data_usage_warning_editor_title).setView(mView).setPositiveButton(R.string.data_usage_cycle_editor_positive,this).create();}private void setupPicker(EditText bytesPicker,Spinner type){final BillingCycleSettings target=(BillingCycleSettings)getTargetFragment();final NetworkPolicyEditor editor=target.services.mPolicyEditor;final NetworkTemplate template=getArguments().getParcelable(EXTRA_TEMPLATE);final boolean isLimit=getArguments().getBoolean(EXTRA_LIMIT);final long bytes=isLimit? editor.getPolicyLimitBytes(template):editor.getPolicyWarningBytes(template);final long limitDisabled=isLimit? LIMIT_DISABLED:WARNING_DISABLED;if(bytes>1.5f*GB_IN_BYTES){bytesPicker.setText(formatText(bytes/(float)GB_IN_BYTES));type.setSelection(1);}else{bytesPicker.setText(formatText(bytes/(float)MB_IN_BYTES));type.setSelection(0);}}private String formatText(float v){v=Math.round(v*100)/100f;//Math.round(float a);将参数加上0.5后返回与参数最近的整数//其实就是去小数部分取整数部分然后再加一return String.valueOf(v);//取float型变量的String类型}@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stubif(arg1!=DialogInterface.BUTTON_POSITIVE){return;}final BillingCycleSettings target=(BillingCycleSettings)getTargetFragment();final NetworkPolicyEditor editor=target.services.mPolicyEditor;final NetworkTemplate template=getArguments().getParcelable(EXTRA_TEMPLATE);final boolean isLimit=getArguments().getBoolean(EXTRA_LIMIT);EditText bytesField=(EditText)mView.findViewById(R.id.bytes);Spinner spinner=(Spinner)mView.findViewById(R.id.size_spinner);String bytesString=bytesField.getText().toString();if(bytesString.isEmpty()||bytesString.equals(".")){bytesString="0";}final long bytes=(long)(Float.valueOf(bytesString)*(spinner.getSelectedItemPosition()==0? MB_IN_BYTES:GB_IN_BYTES));if(isLimit){editor.setPolicyLimitBytes(template,bytes);}else{editor.setPolicyWarningBytes(template,bytes);}target.updatePrefs();}}public static class CycleEditorFragment extends DialogFragment implementsDialogInterface.OnClickListener{private static final String EXTRA_TEMPLATE="template";private NumberPicker mCycleDayPicker;public static void show(BillingCycleSettings parent){if(!parent.isAdded()) return;final Bundle args=new Bundle();args.putParcelable(EXTRA_TEMPLATE, parent.mNetworkTemplate);final CycleEditorFragment dialog=new CycleEditorFragment();dialog.setArguments(args);//这个方法可以保存Fragment的相关信息,同样是在Fragment中通过getArguments获取保存的信息dialog.setTargetFragment(parent, 0);//这个方法可以传递parent给dialog对应的类CycleEditorFragment,我们可以在CycleEditorFragment中通过getTargetFragment()获取parent实例dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);//dialog这个Fragment将会加入到parent.getFragmentManager()得到的FragmentManager中}@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {final Context context=getActivity();//Fragment中获取相关活动的方法final BillingCycleSettings target=(BillingCycleSettings)getTargetFragment();final NetworkPolicyEditor editor=target.services.mPolicyEditor;final AlertDialog.Builder builder=new AlertDialog.Builder(context);final LayoutInflater dialogInflater=LayoutInflater.from(builder.getContext());final View view=dialogInflater.inflate(R.layout.data_usage_cycle_editor, null,false);mCycleDayPicker=view.findViewById(R.id.cycle_day);final NetWorkTemplate template=getArguments().getParcelable(EXTRA_TEMPLATE);//在这里获取上一个Fragment保存的信息final int cycleDay=editor.getPolicyCycleDay(template);final int cycleDayValue=cycleDay;if(savedInstanceState!=null){cycleDayValue=savedInstanceState.getInt("cycleDayValue");}mCycleDayPicker.setMinValue(1);mCycleDayPicker.setMaxValue(31);mCycleDayPicker.setValue(cycleDayValue);mCycleDayPicker.setWrapSelectorWheel(true);return builder.setTitle(R.string.data_usage_cycle_editor_title).setView(view).setPositiveButton(R.string.data_usage_cycle_editor_positive,this).create();}@Overridepublic void onSaveInstanceState(Bundle outState) {// TODO Auto-generated method stubsuper.onSaveInstanceState(outState);outState.putInt("cycleDayValue", mCycleDayPicker.getValue());}@Overridepublic void onClick(DialogInterface arg0, int arg1) {final NetworkTemplate template=getArguments().getParcelable(EXTRA_TEMPLATE);final BillingCycleSettings target=(BillingCycleSettings)getTargetFragment();//获取之前传递给CycleEditorFragment的BillingCycleSettings类的对象final NetworPolicyEditor editor=target.services.mPolicyEditor;mCycleDayPicker.clearFocus();final int cycleDay=mCycleDayPicker.getValue();final String cycleTimezone=new Time().timezone;editor.setPolicyCycleDay(template,cycleDay,cycleTimezone);target.updatePrefs();	}}public static class ConfirmLimitFragment extends DialogFragment implementsDialogInterface.OnClickListener{private static final String EXTRA_MESSAGE="message";private static final String EXTRA_LIMIT_BYTES="limitBytes";public static final float FLOAT=1.2f;public static void show(BillingCycleSettings parent){if(!parent.isAdded()) return;final NetworkPolicy policy=parent.services.mPolicyEditor.getPolicy(parent.mNetworkTemplate);if(policy==null)return;final Resources res=parent.getResources();final CharSequence message;final long minLimitBytes=(long)(policy.warningBytes*FLOAT);final long limitBytes;message=res.getString(R.string.data_usage_limit_dialog_mobile);limitBytes=Math.max(5*GB_IN_BYTES, minLimitBytes);final Bundle args=new Bundle();args.putCharSequence(EXTRA_MESSAGE, message);args.putLong(EXTRA_LIMIT_BYTES, limitBytes);final ConfirmLimitFragment dialog=new ConfirmLimitFragment();dialog.setArguments(args);dialog.setTargetFragment(parent, 0);dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);}@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {final Context context=getActivity();final CharSequence message=getArguments().getCharSequence(EXTRA_MESSAGE);return new AlertDialog.Builder(context).setTitle(R.string.data_usage_limit_dialog_title).setMessage(message).setPositiveButton(android.R.string.ok,this).setNegativeButton(android.R.string.cancel,this).create();}@Overridepublic void onClick(DialogInterface dialog,int which){final BillingCycleSettings target=(BillingCycleSettings)getTargetFragment();if(which!=DialogInterface.BUTTON_POSITIVE){if(target!=null){target.updatePrefs();}return;}final long limitBytes=getArgument().getLong(EXTRA_LIMIT_BYTES);if(target!=null){target.setPolicyLimitBytes(limitBytes);}}}}

这篇关于PreferenceFragment DialogFragment Spinner NumberPicker的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1099318

相关文章

Spinner使用方法

Android中的Spinner和VC中的列表框很类似。这里做个小例子,是为笔记。 示例效果 下拉的效果: 布局文件 Activity的布局my_layout.xml <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/andr

DialogFragment 仿IOS弹框简单实现

主类:先上个图 非常简单,直接上代码: 1.主类: package com.jgw.supercode.ui.fragment;import android.app.AlertDialog;import android.app.Dialog;import android.content.DialogInterface;import android.os.Bundle;imp

自定义控件 - DialogFragment常用库

转载请标明出处:http://blog.csdn.net/u013254166/article/details/79283118 本文出自: 【rhino博客】  直接上效果图,实现很简单,这里就不赘述了。 最后附上源码下载链接,点击下载。

android:详细解读DialogFragment

相信看这篇文章的人都应该知道android中的Dialog了吧,如果对于Dialog还不是很了解可以看我之前的一篇详解文章: Dialog详解:http://www.cnblogs.com/tianzhijiexian/p/3867731.html 随着Fragment这个类的引入,Google官方推荐大家使用DialogFragment来代替传统的Dialog,那么是不是说我们之前学习

ViewPager+DialogFragment实现弹窗左右滑动切换

开发中可能需要作出需要弹出浮层给用户展示一些信息,并且需要浮层能够左右滑动切换的功能。 这就需要用到ViewPager+DialogFragment来实现了,可以随意添加fragment,并且能够左右滑动,随时关闭,也可以定位到打开时在想要有限展示的fragment。 下边是截图和代码: 主界面MainActivity: public class MainAc

DialogFragment NumberPicker的使用

DialogFragment参考博文: http://www.cnblogs.com/tianzhijiexian/p/4161811.html package com.example.util;import android.app.AlertDialog;import android.app.Dialog;import android.app.DialogFragment;impo

DialogFragment——解决PopupWindow中的输入框无法复制粘贴的问题;Android中的两种弹窗PopupWindow和Dialog的区别。

本文旨在介绍Android中的两种弹窗PopupWindow和Dialog的区别。以及关于写代码的一些思考。 其实之前各类弹窗,都是使用PopupWindow来实现的。因为PopupWindow能实现这些需求,虽然有的时候有些麻烦,比如前面的文章有写到PopupWindow去实现蒙版遮罩的问题。它本身是没有蒙版提供的。 我们项目中使用各种方法去给它增加遮盖层。如今看来确实是有些愚蠢的。我不介意

Activity-Spinner

http://www.oschina.net/question/54100_33284 例子一:Spinner的基本实现方法 步骤一:编写Android XML文件 <LinearLayout …… >    <TextView android:id="@+id/spinner_label" …… />     <!-- 经过我们的测试android:drawSelecto

spinner(中)

下拉列表(Spinner)是一个每次只能选择所有项中一项的部件。它的项来自于与之相关联的适配器中。XML AttributesAttribute Name Related Method Descriptionandroid:gravity setGravity(int) 设置当前被选中的项目的位置android:prompt 在下拉列表对话框显示时显示intgetBaseline(

动态添加删除Spinner菜单项

Spinner是一种下接菜单,类似HTML中的select标签,点击后弹出一个对话框,显示几个供选择的选项,手机屏幕大小有限,如果都用RadioGroup单选按钮,会占用很大的空间。今天的例子最终效果如下图: Spinner需要绑定一个适配器ArrayAdapter,将菜单项放在适配器中,添加删除菜单项只需要调用适配器的add,remove方法即可。 布局XML: 01 <?