主布局中的 Android 首选项
我的偏好有问题。 我创建了这个简单的项目: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
SharedPreferences
和Spinner
尝试类似的操作,而不是沿着这些路线的操作应该更适合您正在做的事情。
try something like this using
SharedPreferences
andSpinner
insteadsomething along those lines should be neater for what you are doing.