主布局中的 Android 首选项

发布于 2024-11-05 06:39:18 字数 3360 浏览 1 评论 0原文

我的偏好有问题。 我创建了这个简单的项目: start.java:Preferences.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);      
    }
}

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

主要活动是开始,我们从这里(通过按钮)进入 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>

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

我想直接放入主要首选项(而不是按钮)并使用一个活动

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

I have a problem with the preferences.
I created this simple project :
start.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);
    }
}

the main activity is the start, from which we go (via a button) to Preferences.
Preferences display the preferences( a list with a pop-up) .

Layout :
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>

in values i put 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>

Finally, I created a preferences.xml (in a folder xml in res) :

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

then: I have a main and then I go to a page of Preferences.

I would like to put directly into the main preferences (instead of the button) and use one Activity

but I can not do it in any way ..
please you have any suggestions?
thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剑心龙吟 2024-11-12 06:39:18

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

沿着这些路线的操作应该更适合您正在做的事情。

try something like this using SharedPreferences and Spinner instead

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

something along those lines should be neater for what you are doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文