我已经设置了我的设置活动和我的菜单(因为我正在使用API 10,我做了没有碎片的旧方法)我的菜单有2个设置(进入活动设置就好了)和显示设置,这个有显示自定义Toast设置值。到目前为止,这是我的代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_options_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.config:
startActivity(new Intent(MainActivity.this,
OpcionesActivity.class
));
return true;
case R.id.mconfig:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_layout_id));
TextView text = (TextView) layout.findViewById(R.id.text);
TextView text1 = (TextView) layout.findViewById(R.id.text1);
TextView text2 = (TextView) layout.findViewById(R.id.text2);
// Toast...
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
这是我的自定义Toast XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_layout_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF"
android:orientation="horizontal"
android:padding="5dp" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000" />
</LinearLayout>
如何访问设置值以及我应该使用哪种方法? .setText();
我有一个OpcionesActivity类,如下所示:
public class OpcionesActivity extends PreferenceActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
settings.xml看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="General">
<CheckBoxPreference
android:key="opcion1"
android:title="Sonido"
android:summary="Sonido" />
<EditTextPreference
android:key="opcion2"
android:title="Usuario"
android:summary="Nombre de usuario"
android:dialogTitle="Nombre" />
<ListPreference
android:key="opcion3"
android:title="Dificultad"
android:summary="Dificultad"
android:dialogTitle="Dificultad"
android:entries="@array/d"
android:entryValues="@array/dif" />
</PreferenceCategory>
</PreferenceScreen>
答案 0 :(得分:0)
在创建吐司之前添加此内容
SharedPreferences preferences = getApplication().getSharedPreferences("your prefrens name",MODE_PRIVATE);
text.setText(preferences.getString("key","defaultValue"));
text1.setText(preferences.getString("key","defaultValue"));
text2.setText(preferences.getString("key","defaultValue"));