我已经在XML中定义了一个PreferenceScreen,其中包含几个EditTextPreference“对象”。我想从这些字段中捕获用户输入,但我无法弄清楚如何。答案似乎就在这里,但我没有得到它:http://developer.android.com/reference/android/preference/EditTextPreference.html
我猜这与此类似:
AlertDialog.Builder alert = new AlertDialog.Builder ( this );
final EditText input = new EditText ( this );
alert.setView ( input );
alert.setPositiveButton ( "Ok", new DialogInterface.OnClickListener () {
public void onClick ( DialogInterface dialog, int whichButton ) {
c.setName ( input.getText ().toString () );
}
} );
答案 0 :(得分:1)
与其他Activity一样,您可以将PreferenceScreen用作PreferenceActivity。它的相应类如下所示:
public class MyPreferencesActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);// point your xml file
}
}
不要忘记将此类注册为AndroidManifest.xml文件中的活动。
要显示“首选项”屏幕,只需将其称为常用活动:
Intent i = new Intent(OverviewActivity.this, MyPreferencesActivity.class);
startActivity(i);
您可以将其值视为
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String username = preferences.getString("your_key", "default_value");
如需更多参考,Hava请查看http://www.vogella.com/articles/AndroidFileBasedPersistence/article.html#tutorial_preferenceactivity