public boolean onPreferenceChange(Preference preference, Object newValue) {
String key = preference.getKey();
Log.v(TAG, key + " preference changed");
if (key.equals(connectionServerIpKey)) {
editTextPreferenceServerIP.setSummary(editTextPreferenceServerIP
.getEditText().getText());
} else if (key.equals(connectionServerPortKey)) {
editTextPreferenceServerPort
.setSummary(editTextPreferenceServerPort.getEditText()
.getText());
} else if (key.equals(connectionTypeKey)) {
String value = (String) newValue;//获取最近的值
int index = listPreferenceConnectType.findIndexOfValue(value);//获取该值对应的显示文字在Entry中的索引
CharSequence[] entries = listPreferenceConnectType.getEntries();
listPreferenceConnectType.setSummary(entries[index]);//设置Summary
} else {
return false;
}
return true;
}
当然还有其他的办法,比如有在PreferenceActivity中
public static String PRFS_NAME="PreferenceSettings";
以下为onCreate()函数中
getPreferenceManager().setSharedPreferencesName(PRFS_NAME);
addPreferencesFromResource(R.xml.preferences);在要调用的Activity中使用
SharedPreferences preferences = this.getSharedPreferences(SettingActivity.PRFS_NAME,
MODE_WORLD_READABLE);
Boolean flag = preferences.getBoolean(
getResources().getString(R.string.preferences_exit_confirm),
true);
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
exitDialog();//按下返回键相应的操作
}
return false;
}
private ListBox ListBox1 = new ListBox();
private void InitializeListBox()
{
ListBox1.Items.AddRange(new Object[]
{ "Red Item", "Orange Item", "Purple Item" });
ListBox1.Location = new System.Drawing.Point(81, 69);
ListBox1.Size = new System.Drawing.Size(120, 95);
ListBox1.DrawMode = DrawMode.OwnerDrawFixed;//1
ListBox1.DrawItem += new DrawItemEventHandler(ListBox1_DrawItem);//2
Controls.Add(ListBox1);
}
//3
private void ListBox1_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
// 为listbox的每一项绘制背景
e.DrawBackground();
// 设置默认颜色为黑色
Brush myBrush = Brushes.Black;
if(e.Index<0) return;//索引小于0则退出
// 设置每一项的颜色
switch (e.Index)
{
case 0://设置第一项为红色
myBrush = Brushes.Red;
break;
}
// 绘制listbox中的每一项
e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(),
e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
// 绘制被选中项选中边框
e.DrawFocusRectangle();
}程序运行如图
(int) Long.parseLong(hex, 16);