注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

是缘是情是童真

如果你用笑脸面对 你会看见别人的笑脸

 
 
 

日志

 
 

PreferenceActivity以及数据保存和dialog设置  

2011-06-25 16:04:47|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
public class VMSSetActivity extends PreferenceActivity implements
OnPreferenceClickListener, OnPreferenceChangeListener {
private Preference vms_play, vms_max_duration, vms_pwd;
private CheckBoxPreference vms_voice;
private static final String VMS_PLAY = "vms_play", VMS_VOICE = "vms_voice",
SHARE_NAME = "vms_setting", TAG = "VMSSetActivity",
VMS_MAX_DURATION = "vms_max_duration", VMS_PWD = "vms_pwd";
private static int play_state = 0;
private String[] play_items;
private SharedPreferences vms_settings;
private String voice, play , password , duration ;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// PreferenceActivity 界面比较特别。此布局放置在res/xml文件夹下
addPreferencesFromResource(R.xml.vms_settings);

Log.d(TAG, TAG + "->onCreate");

vms_voice = (CheckBoxPreference) findPreference(VMS_VOICE);
vms_voice.setOnPreferenceChangeListener(this);

vms_play = findPreference(VMS_PLAY);
vms_play.setOnPreferenceClickListener(this);

vms_max_duration = findPreference(VMS_MAX_DURATION);
vms_max_duration.setOnPreferenceClickListener(this);

vms_pwd = findPreference(VMS_PWD);
vms_pwd.setOnPreferenceClickListener(this);

prepare();

}

// 数据初始化
private void prepare() {
play_items = new String[] { getString(R.string.vms_speaker),
getString(R.string.vms_receiver) };
vms_settings = getSharedPreferences(SHARE_NAME,
Context.MODE_WORLD_READABLE);
voice = vms_settings.getString(VMS_VOICE, "");
if (voice != null) {
vms_voice.setSummary(voice);
}

play = vms_settings.getString(VMS_PLAY, "");
for (int i = 0; i < play_items.length; i++) {
if (play != null && play.equals(play_items[i])) {
play_state = i;
vms_play.setSummary(play);
}
}
duration = vms_settings.getString(VMS_MAX_DURATION, "");
if (duration != null && !duration.equals("")) {
vms_max_duration.setSummary(duration+"秒");
} else{
duration = 30+"";
vms_max_duration.setSummary(duration+"秒");
}
password = vms_settings.getString(VMS_PWD, "");
Log.d(TAG, password);
}

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// 声音开关选择
if (preference.getKey().equals(VMS_VOICE)) {
vms_voice.setChecked((Boolean) newValue);
vms_voice.setSummary((Boolean) newValue ? "checked" : "unchecked");
voice = (Boolean) newValue ? getString(R.string.vms_voice_on)
: getString(R.string.vms_voice_off);
cacheSet(VMS_VOICE, voice);
}
return false;
}

@Override
public boolean onPreferenceClick(Preference preference) {
// 播放器选择
if (preference.getKey().equals(VMS_PLAY)) {
new AlertDialog.Builder(this).setTitle(
getResources().getString(R.string.vms_play_choose))
.setSingleChoiceItems(play_items, play_state,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {

play = which == 0 ? getResources()
.getString(R.string.vms_speaker)
: getResources().getString(
R.string.vms_receiver);
vms_play.setSummary(play);
cacheSet(VMS_PLAY, play);
play_state = which;
dialog.dismiss();
}
}).setNegativeButton(
getResources().getString(R.string.cancel), null)
.show();
} else if (preference.getKey().equals(VMS_MAX_DURATION)) {
LayoutInflater inflater = getLayoutInflater();
final View layout = inflater.inflate(R.layout.duration_edit,
(ViewGroup) findViewById(R.id.duration));
final SeekBar sb = (SeekBar) layout.findViewById(R.id.sb);
final TextView tv_value = (TextView) layout.findViewById(R.id.tv_value);
if (duration!=null && !duration.equals("")) {
if (Integer.parseInt(duration)-30>=0) {
tv_value.setText(duration+"秒");
sb.setProgress(Integer.parseInt(duration)-30);
}
}
if (tv_value==null) {
Toast.makeText(VMSSetActivity.this,
"null",
Toast.LENGTH_LONG).show();
}
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
// 拖动中
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// 当前值
// System.out.println(progress);

}

// 开始拖动
@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

// 结束拖动
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int durationTime = seekBar.getProgress()+30;
duration = durationTime+"";
String second = durationTime+"秒";
tv_value.setText(second);
}
});
new AlertDialog.Builder(this).setTitle(
getString(R.string.vms_choose_duration)).setView(layout)
.setPositiveButton(getString(R.string.vms_ok),
new OnClickListener() {

public void onClick(DialogInterface dialog,
int position) {
cacheSet(VMS_MAX_DURATION, duration+"");
vms_max_duration.setSummary(duration+"秒");
}
}).setNegativeButton(
getString(R.string.vms_cancel), new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
duration = vms_settings.getString(VMS_MAX_DURATION, "");
}
}).show();

} else if (preference.getKey().equals(VMS_PWD)) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.pwd_edit,
(ViewGroup) findViewById(R.id.dialog));
final EditText pwd = (EditText) layout.findViewById(R.id.pwd);
final EditText pwd_again = (EditText) layout
.findViewById(R.id.pwd_again);
new AlertDialog.Builder(this).setTitle(
getString(R.string.vms_change_pwd)).setView(layout)
.setPositiveButton(getString(R.string.vms_ok),
new OnClickListener() {

public void onClick(DialogInterface dialog,
int position) {
String pwdStr = pwd.getText().toString();
String pwdagainStr = pwd_again.getText()
.toString();
if (pwdStr.length() > 0
&& pwdStr.equals(pwdagainStr)) {
Toast.makeText(VMSSetActivity.this,
pwdagainStr, Toast.LENGTH_LONG)
.show();
cacheSet(VMS_PWD, pwdagainStr);
setDialogDismiss(dialog, true);
} else {
Toast.makeText(VMSSetActivity.this,
R.string.vms_pwd_error,
Toast.LENGTH_LONG).show();
setDialogDismiss(dialog, false);
}
}
}).setNegativeButton(
getString(R.string.vms_cancel),
new OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int arg1) {
setDialogDismiss(dialog, true);
}
}).show();
}
return false;
}

// 设置对话框不消失和消失的情形
private void setDialogDismiss(DialogInterface dialog, boolean showing) {
try {
Field field = dialog.getClass().getSuperclass().getDeclaredField(
"mShowing");
field.setAccessible(true);
// 将mShowing变量设为false,表示对话框已关闭
field.set(dialog, showing);
dialog.dismiss();

} catch (Exception e) {
Log.d(TAG, e.getMessage());
}
}

// 将设置信息缓存到本地
public void cacheSet(String category, String value) {
Editor editor = vms_settings.edit();
if (category.equals(VMS_VOICE)) {
editor.putString(VMS_VOICE, value);
} else if (category.equals(VMS_PLAY)) {
editor.putString(VMS_PLAY, value);
}else if (category.equals(VMS_MAX_DURATION)) {
editor.putString(VMS_MAX_DURATION, value);
}else if (category.equals(VMS_PWD)) {
editor.putString(VMS_PWD, value);
}
editor.commit();

}

}

pwd.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:orientation="vertical" android:id="@+id/dialog"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<RelativeLayout android:id="@+id/RelativeLayout02"
android:layout_width="fill_parent" android:layout_height="wrap_content" >

<EditText android:id="@+id/pwd"  
android:paddingLeft="90.0dip"
android:saveEnabled="true" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_marginLeft="2.0dip"
android:layout_marginRight="2.0dip" android:layout_marginBottom="5.0dip" 
android:textSize="14.0sp" 
android:maxLength="16" 
android:password="true"
android:layout_below="@+id/RegisterTitle" />
<TextView android:id="@+id/TextView01"
android:textSize="16.0sp" android:textColor="#ff3f3f3f"
android:gravity="center_vertical" 
android:paddingLeft="12.0dip" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginRight="20.0dip"
android:text= "@string/vms_password"
android:layout_alignLeft="@+id/pwd"
android:layout_alignTop="@+id/pwd"
android:layout_alignBottom="@+id/pwd"/>
<EditText android:id="@+id/pwd_again"  
android:paddingLeft="90.0dip"
android:saveEnabled="true" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_marginLeft="2.0dip"
android:layout_marginRight="2.0dip" 
android:textSize="14.0sp"
android:maxLength="16" 
android:password="true"
android:layout_below="@+id/pwd"/>
<TextView android:id="@+id/TextView02" 
android:textSize="16.0sp" android:textColor="#ff3f3f3f"
android:gravity="center_vertical" 
android:paddingLeft="12.0dip" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginRight="20.0dip"
android:text="@string/vms_password_again"
android:layout_alignLeft="@+id/pwd_again"
android:layout_alignBottom="@+id/pwd_again"
android:layout_alignTop="@+id/pwd_again"/>
</RelativeLayout>
</LinearLayout>

duration.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:orientation="vertical" android:id="@+id/duration"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<SeekBar android:id="@+id/sb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="90" 
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
/>
<TextView android:id="@+id/tv_value" android:layout_width="fill_parent"  
        android:layout_height="wrap_content" android:text="@string/vms_duration" 
        android:layout_marginLeft="5dp"/>  
</LinearLayout>

setting.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="@string/set">
<CheckBoxPreference  
            android:key="vms_voice"  
            android:title="@string/vms_set_voice"  
            android:summaryOn="@string/vms_voice_on"  
            android:summaryOff="@string/vms_voice_off"  
            android:defaultValue="true"  
        />  

        <Preference
        android:key="vms_play"  
            android:title="@string/vms_set_play"  
            android:summary="@string/vms_speaker"   
             >
        </Preference>
        <Preference  
            android:key="vms_max_duration"  
            android:title="@string/vms_max_duration"  
            android:summary="@string/vms_duration"   
        />  
         <Preference  
            android:key="vms_pwd"  
            android:title="@string/vms_pwd"  
        />  
</PreferenceCategory>
</PreferenceScreen>
  评论这张
 
阅读(1120)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018