Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android “访问编辑文本首选项”对话框_Android_Dialog_Sharedpreferences_Edittextpreference - Fatal编程技术网

Android “访问编辑文本首选项”对话框

Android “访问编辑文本首选项”对话框,android,dialog,sharedpreferences,edittextpreference,Android,Dialog,Sharedpreferences,Edittextpreference,我不知道使用SharedReferences应该把代码放在哪里。我试图找出对话框是如何创建的。我想访问EditTextPreference对话框,一旦用户输入他们的姓名,我想使用下面的代码将其保存到一个文件中 SharedPreferences mySharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE); SharedPreferences.Editor editor = my

我不知道使用SharedReferences应该把代码放在哪里。我试图找出对话框是如何创建的。我想访问EditTextPreference对话框,一旦用户输入他们的姓名,我想使用下面的代码将其保存到一个文件中

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




    }
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();

}
这是我的UserPreferenceActivity 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




    }
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>

使用首选项类的方法和
首选项.OnPreferenceChangeListener
接口。大概是这样的:

pref_general.xml:
使用Preference类和Preference.OnPreferenceChangeListener接口的
setOnPreferenceChangeListener
方法。大概是这样的:

pref_general.xml:
发布这个类代码
UserPreferenceFragment
oopps我在代码中有它,但它显示为代码。它现在就在那里。发布这个类代码
UserPreferenceFragment
oopps我在代码中有它,但它显示为代码。它现在在那里。