0
我很困惑我應該在哪裏使用sharedPreferences放置此代碼。我試圖找出如何創建對話框。我想訪問EditTextPreference對話框,一旦用戶輸入他們的名字,我想使用此代碼,將其保存到一個文件下面訪問EditText首選項對話框
SharedPreferences mySharedPreferences = getSharedPreferences("MyData",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString("user_name",userName.getText().toString());
editor.commit();
這是我的主要活動
public class MainActivity extends AppCompatActivity {
EditText userName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
Button myButton = (Button) findViewById(R.id.show_button_id);
myButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent i = new Intent(MainActivity.this,UserPreferenceActivity.class);
startActivity(i);
//return true;
}
return super.onOptionsItemSelected(item);
}
}
這裏是我的UserPreferenceFragment Java類
public class UserPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//LOAD THE PRFERENCE FROM AN XML RESOURCE FILE
addPreferencesFromResource(R.xml.preferences);
}//ends OnCreate
}
這裏是我的UserPreferenceActivity java類
public class UserPreferenceActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
setContentView(R.layout.content_main);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.
*/
getFragmentManager().beginTransaction()
.replace(android.R.id.content,new UserPreferenceFragment())
.commit();
}
這裏是我的喜好的xml文件
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="show_background_pic"
android:title="@string/show_background_pic_title"
android:summary="@string/show_background_pic_summary"
android:defaultValue="true">
</CheckBoxPreference>
<EditTextPreference
android:key="user_name"
android:title="@string/user_acct_name_title"
android:summary="@string/user_acct_name_summary"
android:defaultValue="NONAME">
</EditTextPreference>
最後我的主要內容
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/Shared_preferences_label"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_button_label"
android:id="@+id/show_button_id"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true" />
</RelativeLayout>
後這個類代碼'UserPreferenceFragment' – Bharatesh
oopps我有它的代碼,但它被顯示爲代碼。它現在在那裏。 – Carlitos