【问题标题】:android preferences in main layout主布局中的 android 首选项
【发布时间】:2011-05-04 07:09:52
【问题描述】:

我的偏好有问题。 我创建了这个简单的项目: 开始.java:

package example.ex;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class start extends Activity {
    Intent intent;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void preferences (View view){
        intent = new Intent(getApplicationContext(), Preferences.class);
        startActivity(intent);      
    }
}

Preferences.java:

    package example.ex;

import android.os.Bundle;
import android.preference.PreferenceActivity;

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

主要活动是开始,我们从这里开始(通过一个按钮)到首选项。 首选项显示首选项(带有弹出窗口的列表)。

布局: main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="preferences"
     android:onClick="preferences" />
</LinearLayout>

preferenze.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
    <TableRow>
        <TextView
                android:text="List Selection:"
                android:paddingRight="5px"
        />
        <TextView android:id="@+id/list"
        />
    </TableRow>
</TableLayout>

在值中我放入arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="country">
        <item>U.S. (United States of America)</item>
        <item>Italia (Italy)</item>
    </string-array>
    <string-array name="codice_paese">
        <item>US</item>
        <item>IT</item>
    </string-array>
</resources>

最后,我创建了一个preferences.xml(在res中的一个xml文件夹中):

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="Simple Preferences">
        <ListPreference android:key="list" android:title="Selection Dialog"
            android:summary="Click to pop up a list to choose from"
            android:entries="@array/country" android:entryValues="@array/codice_paese"
            android:dialogTitle="Choose a country" />
    </PreferenceCategory>
</PreferenceScreen>

然后:我有一个主页面,然后我转到一个首选项页面。

我想直接放入主要偏好设置(而不是按钮)并使用一个 Activity

但我不能以任何方式做到这一点.. 请问您有什么建议吗? 谢谢!

【问题讨论】:

    标签: android preferences


    【解决方案1】:

    改用SharedPreferencesSpinner 尝试类似的操作

    package com.example.spinnerpref;
    
    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Spinner;
    import android.widget.AdapterView.OnItemSelectedListener;
    
    public class SpinnerPref extends Activity
    {
    int countrypos;
    public void onCreate(Bundle savedInstanceState)
        {
        super.onCreate(savedInstanceState);
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        countrypos = prefs.getInt("countrypos", 0);
        setContentView(R.layout.main);
    
        Spinner countryspinner = (Spinner) findViewById(R.id.country);
        ArrayAdapter<CharSequence> countryadapater = ArrayAdapter.createFromResource(
                this, R.array.country, android.R.layout.simple_spinner_item);
        countryadapater.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        countryspinner.setAdapter(countryadapater);
        countryspinner.setOnItemSelectedListener(new countrySelector());
    
        class countrySelector implements OnItemSelectedListener
        {
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
                {
                countrypos = pos;
                }
            public void onNothingSelected(AdapterView<?> parent) {}
        }
    
        PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
            .edit().putInt("countrypos", countrypos).commit();
        }
    }
    

    这些方面的东西应该更适合你正在做的事情。

    【讨论】:

    • 确保相应地更新 main.xml 和 strings.xml。我知道我对此不是很彻底,但我不想花很多时间在这上面,以免你甚至可能看不到它/不再需要它,所以如果你对它有任何疑问,请告诉我和不好澄清。
    猜你喜欢
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    • 1970-01-01
    相关资源
    最近更新 更多