gpt4 book ai didi

android - PreferenceActivity 可以自定义它处理触摸的方式吗?

转载 作者:行者123 更新时间:2023-11-30 02:23:00 26 4
gpt4 key购买 nike

我最近接触了 PreferenceActivity,我想改变我处理与 xml 中定义的 EditTextPreference 交互的方式。

我已经在样板文件覆盖 onListItemClick( 的地方放置了日志、toast 和断点,但没有任何回复。我什至尝试进入父类(super class)并能够设置断点在 ifreturn 上成功,尽管它们最终并未陷入困境。

protected void onListItemClick(ListView l, View v, int position, long id) {
if (!isResumed()) {
return;
}
super.onListItemClick(l, v, position, id);

感谢收看

编辑 @DanielLe,这是我的代码:

//This isn't getting called?!
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String selection = l.getItemAtPosition(position).toString();
Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
Log.d("Activity", "onListItemClick=" + l.getItemAtPosition(position).toString());
super.onListItemClick(l, v, position, id);
}

最佳答案

冒着重复之前发生的事情的风险,一种解决方案是扩展 DialogPreferences,如 Android 开发者的 Google 指南中所述。它只显示确定和取消按钮,我相信这是持久化 DialogPreferences 实现的最小值:

Android: Creating custom preference

import android.content.Context;
import android.content.DialogInterface;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.DialogPreference;
import android.preference.EditTextPreference;
import android.util.AttributeSet;
import android.widget.Button;
import android.widget.EditText;

public class ClickablePreference extends DialogPreference {
private String mNewValue;
private EditText mEditText;

public ClickablePreference(Context context, AttributeSet attrs) {
super(context, attrs);

setDialogLayoutResource(R.layout.dir_picker_dialog);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);

setDialogIcon(null);
}

@Override
protected void onDialogClosed(boolean positiveResult) {
// When the user selects "OK", persist the new value
if (positiveResult) {
persistString(mNewValue);
}
}

private static class SavedState extends BaseSavedState {
// Member that holds the setting's value
// Change this data type to match the type saved by your Preference
String value;

public SavedState(Parcelable superState) {
super(superState);
}

public SavedState(Parcel source) {
super(source);
// Get the current preference's value
value = source.readString(); // Change this to read the appropriate data type
}

@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
// Write the preference's value
dest.writeString(value); // Change this to write the appropriate data type
}

// Standard creator object using an instance of this class
public static final Parcelable.Creator<SavedState> CREATOR =
new Parcelable.Creator<SavedState>() {

public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}

public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}

@Override
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
if (restorePersistedValue) {
// Restore existing state
mNewValue = this.getPersistedString("");
} else {
// Set default state from the XML attribute
mNewValue = (String) defaultValue;
persistString(mNewValue);
}
}

@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
// Check whether this Preference is persistent (continually saved)
if (isPersistent()) {
// No need to save instance state since it's persistent,
// use superclass state
return superState;
}

// Create instance of custom BaseSavedState
final SavedState myState = new SavedState(superState);
// Set the state's value with the class member that holds current
// setting value
myState.value = mNewValue;
return myState;
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
// Check whether we saved the state in onSaveInstanceState
if (state == null || !state.getClass().equals(SavedState.class)) {
// Didn't save the state, so call superclass
super.onRestoreInstanceState(state);
return;
}

// Cast state to custom BaseSavedState and pass to superclass
SavedState myState = (SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());

// Set this Preference's widget to reflect the restored state
mEditText.setText(myState.value);
}

@Override
protected void onClick() {
super.onClick();
}

@Override
public void onClick(DialogInterface dialog, int which) {
super.onClick(dialog, which);
}
}

我还没看过Concise way of writing new DialogPreference classes?很详细,尽管它看起来与我从 Google 获得的非常相似。

除了它的大小之外,这个解决方案的问题是 mEditText 没有被使用,我实际上无法获取对在 xml 中定义的显示的 EditText 的引用。


继续解决方案 2

感谢Android: launch a custom Preference from a PreferenceActivity

刚刚在 MyPreferenceActivity 的 onPostCreate 结束时标记了这个

protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
setupSimplePreferencesScreen();
Preference customPref = (Preference) findPreference("pref_do_something");
customPref.setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Log.d("Activity", "onPreferenceClick=" + preference.toString());
return true;
}
});
}

好多了:)

关于android - PreferenceActivity 可以自定义它处理触摸的方式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28217350/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com