PreferenceActivity使用方法

PreferenceActivity使用方法    PreferenceActivity使用方法     PreferenceActivity使用方法

 public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(), MyPreferenceActivity.class));
}
});
}
}
 package com.example.preferenceactivity;

 import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.Toast; /**
* 1. CheckBoxPreference.
* 2. ListPreference.
* 3. editTextPreference.
*
* @author dr
*/
public class MyPreferenceActivity extends PreferenceActivity { PreferenceManager manager;
CheckBoxPreference checkBoxPreference;
ListPreference listPreference;
EditTextPreference editTextPreference; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.mypreferenc); manager = getPreferenceManager();
checkBoxPreference = (CheckBoxPreference) manager
.findPreference("checkbox");
Toast.makeText(getApplicationContext(),
"当前的状态为:" + checkBoxPreference.isChecked(), Toast.LENGTH_SHORT)
.show(); listPreference = (ListPreference) manager.findPreference("list");
Toast.makeText(
getApplicationContext(),
listPreference.getEntry() + "的开发环境为:"
+ listPreference.getValue(), Toast.LENGTH_SHORT).show(); editTextPreference = (EditTextPreference) manager
.findPreference("text");
Toast.makeText(getApplicationContext(),
"您输入的内容为:" + editTextPreference.getText(), Toast.LENGTH_SHORT)
.show(); }
}

在res目录下面,新建一个xml文件,里面 mypreferenc.xml。

 <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="checkbox"
android:title="是否开启"
android:summaryOn="已经开启"
android:summaryOff="已经关闭" /> <ListPreference
android:key="list"
android:title="选择第一个选项"
android:summary="请点击选择"
android:entries="@array/entries"
android:entryValues="@array/values" /> <EditTextPreference
android:key="text"
android:dialogTitle="请输入你的名字"
android:dialogMessage="有劳了"
android:summary="请在此输入"
android:title="请输入" /> </PreferenceScreen>

在res/values,里面 mylistpreference.xml。

 <?xml version="1.0" encoding="utf-8"?>
<resources> <string-array name="entries">
<item>Java</item>
<item>Swift</item>
<item>C#</item>
</string-array> <string-array name="values">
<item>Eclips</item>
<item>Xcode</item>
<item>Visual Studio</item>
</string-array> </resources>
上一篇:ACM2 斐波那契数列


下一篇:初识QML学习机制