如何在Android中创建首选项?

时间:2013-05-08 13:27:54

标签: android mobile sharedpreferences

我的android设置有问题。我想创建用于更改活动背景颜色的设置。我该怎么办?

我有布局:

public class MyApp extends PreferenceActivity{


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    }

public void display(View view) 
{
        Intent intent = new Intent(this, Display.class);

    startActivity(intent);

}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{

    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu, menu);
    return true;
}


public boolean onOptionsItemSelected( MenuItem item)
{

    switch (item.getItemId())
    {
    case R.id.menu_about:
        Intent menu_about = new Intent(this, About.class);
         startActivity(menu_about);
        return true;

    case R.id.menu_copyright:
        Intent menu_copyright = new Intent(this, Copyright.class);
        startActivity(menu_copyright);

        return true;

    case R.id.menu_settings:

        // ACTIVITY OF SETTINGS
        return true;

    case R.id.menu_exit:

        finish();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
};

}

我想要这样的偏好:

http://i.stack.imgur.com/k2qA5.png

现在,你有没有想过我?

3 个答案:

答案 0 :(得分:1)

以这种方式创建SharedPreferences

 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
 SharedPreferences.Editor editor = settings.edit();
 editor.putString("PrefName", VALUE);
 editor.commit();

以这种方式获取其价值

 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
 String s = settings.getString("PrefName", ""));

最后一个语句表示您正在寻找“PrefName”的值,如果找不到任何内容则设置“”。

希望有所帮助

答案 1 :(得分:1)

menu.xml

 <?xml version="1.0" encoding="utf-8"?>
        <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item    
        android:id="@+id/settings"    
        android:title="Settings"    
       >
        <menu>
             <item android:id="@+id/red"
                  android:title="Red" />

            <item android:id="@+id/Blue"

                  android:title="Blue" />

    </menu>  
   </item>  

     </menu>

<强> AndroidMenusActivity.java

public class AndroidMenusActivity extends Activity {
   LinearLayout li;
     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            li= findViewById(R.id.layoutid);
        }



  @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
            MenuInflater menuInflater = getMenuInflater();
            menuInflater.inflate(R.layout.menu, menu);
            return true;
        }


  public boolean onOptionsItemSelected(MenuItem item)
    {

        switch (item.getItemId())
        {
        case R.id.red:
              li.setBackgroundColor("#ff0000");
            break;

        case R.id.blue:
              li.setBackgroundColor("#0000ff");
        break;

        }
    }   

}

<强> main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</LinearLayout>

答案 2 :(得分:0)

您的意思是改变XML文件的活动背景吗?

  

机器人:背景= “@机器人:颜色/ XXXXX”

选择一种列出的颜色。