2013-02-28 79 views
3

这绝对是一个菜鸟问题。我按照这里的说明http://developer.android.com/guide/topics/ui/settings.html#Activity,当我点击设置,没有任何反应。将设置添加到Android应用程序

下面是我在MainActivity.java:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    PreferenceManager.setDefaultValues(this, R.xml.preferences, false); 
} 

然后,我有叫PrefsActivity.java

public class PrefsActivity extends PreferenceActivity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    addPreferencesFromResource(R.xml.preferences); 
} } 

然后,我有RES/XML /的preferences.xml

一个新的Java文件
<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 
<CheckBoxPreference 
    android:key="face_up" 
    android:title="@string/face_up" 
    android:summary="@string/face_up_desc" 
    android:defaultValue="false" /> 
</PreferenceScreen> 

如果可能,我试图使其与minsdk 7兼容。我错过了什么?

回答

8

你需要打开你的活动,当你点击你的设置按钮。如果您使用的操作栏,使用这样的:

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

就像一个魅力。我怎么知道这是必需的?有没有更多的信息。谢谢 – batoutofhell 2013-02-28 23:47:18

+3

@batoutofhell我同意,它应该可能在教程中。你有没有经过[培训](http://developer.android.com/training/index.html)课程?应用程序开发的所有基础都在那里。无论何时您想要参加新的活动,您都必须打电话来打开该活动。见[这里](http://developer.android.com/training/basics/firstapp/starting-activity.html)。希望这可以帮助! – 2013-02-28 23:54:49

2

在主要活动

// Ensure the right menu is setup 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    // Start your settings activity when a menu item is selected 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     if (item.getItemId() == R.id.action_settings) { 
      Intent settingsIntent = new Intent(this, PrefsActivity.class); 
      startActivity(settingsIntent); 
     } 
     return super.onOptionsItemSelected(item); 
    } 
相关问题