`
tansitongba
  • 浏览: 503802 次
文章分类
社区版块
存档分类
最新评论

不好意思,记录个东西,非文章

 
阅读更多
http://blog.csdn.net/cz285933169/article/details/6528852
package com.scott.prefs;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.widget.Toast;

public class PrefsActivity extends PreferenceActivity implements Preference.OnPreferenceChangeListener {

private EditTextPreference nickName;
private ListPreference textSize;
private Preference cleanHistory;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.setttings);
nickName = (EditTextPreference) findPreference("nickName");
textSize = (ListPreference) findPreference("textSize");
cleanHistory = findPreference("cleanHistory");

//为nickName和textSize注册Preference.OnPreferenceChangeListener监听事件
//当值更改时我们可以立即更新summary
nickName.setOnPreferenceChangeListener(this);
textSize.setOnPreferenceChangeListener(this);

initSummary();
}

//初始化summary
private void initSummary() {
nickName.setSummary(nickName.getText());

setTextSizeSummary(textSize.getValue());
}

private void setTextSizeSummary(String textSizeValue) {
if (textSizeValue.equals("0")) {
textSize.setSummary("小");
} else if (textSizeValue.equals("1")) {
textSize.setSummary("中");
} else if (textSizeValue.equals("2")) {
textSize.setSummary("大");
}
}

/**
* 重写PreferenceActivity的onPreferenceTreeClick方法
* 在首选项被点击时 做出相应处理操作
*/
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if (preference == cleanHistory) {
new AlertDialog.Builder(this)
.setTitle("清除历史记录")
.setMessage("是否真的要清除历史记录?")
.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//cleaning history...
Toast.makeText(PrefsActivity.this, "清除成功", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show();
}
return true;
}

/**
* 重写Preference.OnPreferenceChangeListener的onPreferenceChange方法
* 当首选项的值更改时 做出相应处理操作
*/
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference == nickName) {
nickName.setSummary(newValue.toString());
} else if (preference == textSize) {
setTextSizeSummary(newValue.toString());
}
return true;
}
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics