如何在设置中显示特定属性

时间:2016-11-17 18:26:37

标签: android android-layout

在我的Android应用程序中,我有一个包含四个属性的设置布局。 您可以通过MainActivityNoticeActivity来调用设置。要做到这一点,我已经覆盖了这个方法:

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
         case R.id.action_settings:
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            return true;
    }
 }

要显示我有这两个类的设置:

public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new SettingsFragment())
            .commit();
   }
}

public class SettingsFragment extends PreferenceFragment {
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       // preference_general -> layout with 4 properties
       addPreferencesFromResource(R.xml.preference_general);
   }
}

我想要做的是:如果从MainActivity调用设置,我想显示所有四个属性,但如果从NoticeActivity调用,我想只显示前三个属性。

如何在不创建第二个布局的情况下执行此操作?

2 个答案:

答案 0 :(得分:0)

您可以通过在两个活动中定义一些常量来实现。例如:

Intent intent = new Intent(this, SettingsActivity.class);
intent.putExtra("Activity","first_activity");
startActivity(intent);

请记住,两个活动中的名称“活动”应该相同。 并在设置活动时使用检查来调用特定设置。例如:

public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   Intent i = getIntent();
   String checkingValue = i.getStringExtra("Activity");

   if(checkingValue != null){   //Or something else
       //setting layout with four properties
   }else{
      //setting layout with three properties
   }
}
}

希望这有帮助!

答案 1 :(得分:0)

我让这个为我工作。不知道它是否编程良好。

public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String activity = getIntent().getStringExtra("Activity"); // Before starting Intent, put class name as Extra

    if (activity != null && activity.equals("Main")) { 
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new SettingsFragment_Main())
                .commit();
    } else {
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new SettingsFragment_Notice())
                .commit();
    }

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    prefs.registerOnSharedPreferenceChangeListener(this);
}

public static class SettingsFragment_Main extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preference_general);
    }
}

public static class SettingsFragment_Notice extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // first set layout, otherwise the preference will be null
        addPreferencesFromResource(R.xml.preference_general);

        // Disable the Preference
        Preference preference = findPreference(Constants.FOLDER_DEST_SETTING_KEY);
        preference.setEnabled(false);
    }
}
}