Listpreference 启动新活动
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.preference.PreferenceActivity;
public class Preferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
SharedPreferences SP = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
String listPref = SP.getString("listPref", "No City Selected");
SP.getString("listPref", "No City Selected");
SP.contains(listPref);
if (listPref == "Aplpharetta") {
Intent myIntent = new Intent(Preferences.this, Contents.class);
Preferences.this.startActivity(myIntent);
} else if ("Greenville".equals("Greenvile")) {
Intent myIntent = new Intent(Preferences.this, Contents.class);
Preferences.this.startActivity(myIntent);
} else if ("Houston".equals("Houston")) {
Intent myIntent = new Intent(Preferences.this, Contents.class);
Preferences.this.startActivity(myIntent);
}}}
这是数组文件
<string-array name="listArray">
<item>Alpharetta</item>
<item>Greenville</item>
<item>Houston</item>
<item>Tampa</item>
</string-array>
<string-array name="listValues">
<item>Alpharetta</item>
<item>Greenville</item>
<item>Houston</item>
<item>Tampa</item>
</string-array>
</resources>
我遇到的问题是在选择列表首选项时启动活动。我尝试了上面列出的两种方法,该列表允许您选择该项目,但不会触发其他活动启动。由于信息填充在应用程序的其他部分,因此数组列表的设置不包含整数。我尝试过 setOnPreferenceChangeListener() 和 setOnPreferenceClickListener() 。使用这些功能时,首选项屏幕会闪烁以进行发送,并立即返回到前一个屏幕,就像正在启动活动一样,但如果您需要进行更改,则不会保持打开足够长的时间。这是我第一次尝试制作偏好屏幕,所以我不确定我是否遗漏了某些内容或完全错误的方式。
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.preference.PreferenceActivity;
public class Preferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
SharedPreferences SP = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
String listPref = SP.getString("listPref", "No City Selected");
SP.getString("listPref", "No City Selected");
SP.contains(listPref);
if (listPref == "Aplpharetta") {
Intent myIntent = new Intent(Preferences.this, Contents.class);
Preferences.this.startActivity(myIntent);
} else if ("Greenville".equals("Greenvile")) {
Intent myIntent = new Intent(Preferences.this, Contents.class);
Preferences.this.startActivity(myIntent);
} else if ("Houston".equals("Houston")) {
Intent myIntent = new Intent(Preferences.this, Contents.class);
Preferences.this.startActivity(myIntent);
}}}
Here is the Arrays file
<string-array name="listArray">
<item>Alpharetta</item>
<item>Greenville</item>
<item>Houston</item>
<item>Tampa</item>
</string-array>
<string-array name="listValues">
<item>Alpharetta</item>
<item>Greenville</item>
<item>Houston</item>
<item>Tampa</item>
</string-array>
</resources>
The problem I am having is making the activity start when a listpreference is selected. I have tried the two ways listed above, the list lets you select the item, but not trigger the other activity to start. The arrays list is set up without integers due to the information being populated in another part of the app. I have tried setOnPreferenceChangeListener() and setOnPreferenceClickListener() . With using those, the preference screen would flash up for a send and return right back to the previous screen like it is starting the activity, but doesn't stay open long enough if you need to make a change. This is my first attempt at making a preference screen, so I am not sure if I am missing something or going about this the entirely wrong way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 APIDemos 以了解您正在使用的 API 版本。有一个偏好活动示例,有很好的解释。您想要查看的 2 个文件是:com.example.android.apis.app 中的 PreferencesFromCode.java 和 PreferencesFromXML.java。
简而言之,您需要注册您想要的特定偏好的意图。
这样,当单击首选项时,它将自动触发您为其注册的活动。
更新:
我已经快速创建了活动并测试了下面的代码。您说过您已经尝试过 OnChangeListener,但由于某种原因它没有成功。这可以使用 OnSharedPreferenceChangeListener 来实现,并且当您更改“list_preference”的值时它会启动意图。
Have a look in the APIDemos for the API version that you a re using. There is a Preference Activity example with good explanations. 2 files you want to have a look at are: PreferencesFromCode.java and PreferencesFromXML.java in com.example.android.apis.app.
In short, you need to register intent for the particular preference you want.
This way when preference is clicked it will automatically fire the Activity you have registered for it.
UPDATE:
I have quickly created the activity and tested code below. You have said that you have tried the OnChangeListener, but for some reason it didn't work out. This works using OnSharedPreferenceChangeListener and it launches the intent when you change the value of the "list_preference".