ListPreference 编码值未在 if 语句内执行
你能检查一下我的代码是否缺少任何内容吗?
我试图在 if 语句中使用列表首选项中的值,但 if 语句永远不会响应存储在列表首选项中的值。
这里是我使用的编码:
String stringChimeVolume = clockSettings.getString("ChimeVolume",
"Default");
Toast.makeText(context, "The preference is: " + stringChimeVolume,
Toast.LENGTH_SHORT).show();
if (stringChimeVolume == "2") {
manager.setStreamVolume(AudioManager.STREAM_MUSIC,
2, AudioManager.FLAG_VIBRATE);
Toast.makeText(context, "The volume is at 2: ",
Toast.LENGTH_SHORT).show();
} else if (stringChimeVolume == "3") {
Toast.makeText(context, "The volume is at 3: ",
Toast.LENGTH_SHORT).show();
manager.setStreamVolume(AudioManager.STREAM_MUSIC,
7, AudioManager.FLAG_VIBRATE);
}
else if (stringChimeVolume == "4") {
manager.setStreamVolume(AudioManager.STREAM_MUSIC,
15, AudioManager.FLAG_VIBRATE);
}
我使用第一个 Toast 语句来确保存在值。它的值为 2,但 if 语句中“2”的 Toast 未执行。
这是我用于列表首选项的 arrays.xml:
<string-array name="chimeVolumeLabels">
<item>Use System Volume</item>
<item>Emad</item>
<item>Medium</item>
<item>Loud</item>
</string-array>
<string-array name="chimeVolumeValues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
这是带有列表首选项的 settings.xml:
<PreferenceCategory android:title="Media:">
<CheckBoxPreference android:key="ChimeWhenMusicIsPlaying"
android:title="@string/ChimeWhenMusicIsPlayingTitle" android:summary="@string/ChimeWhenMusicIsPlayingSummary"
android:defaultValue="false" />
<ListPreference android:title="Chime Volume"
android:key="ChimeVolume" android:summary="Select volume for the chiming sound."
android:entries="@array/chimeVolumeLabels" android:entryValues="@array/chimeVolumeValues"
android:defaultValue="1" />
</PreferenceCategory>
我们将不胜感激所有帮助。
确实, 埃马德
Can you check my code for anything missing?
I'm trying to get use the value from a listpreference in an if statement but the if statement never responds to the value stored in the listpreference.
Here the coding I'm using:
String stringChimeVolume = clockSettings.getString("ChimeVolume",
"Default");
Toast.makeText(context, "The preference is: " + stringChimeVolume,
Toast.LENGTH_SHORT).show();
if (stringChimeVolume == "2") {
manager.setStreamVolume(AudioManager.STREAM_MUSIC,
2, AudioManager.FLAG_VIBRATE);
Toast.makeText(context, "The volume is at 2: ",
Toast.LENGTH_SHORT).show();
} else if (stringChimeVolume == "3") {
Toast.makeText(context, "The volume is at 3: ",
Toast.LENGTH_SHORT).show();
manager.setStreamVolume(AudioManager.STREAM_MUSIC,
7, AudioManager.FLAG_VIBRATE);
}
else if (stringChimeVolume == "4") {
manager.setStreamVolume(AudioManager.STREAM_MUSIC,
15, AudioManager.FLAG_VIBRATE);
}
I used the first Toast statement to ensure a value was there. It has the value of 2 but the Toast in the if statement for "2" is not executing.
Here is the arrays.xml I used for the listpreference:
<string-array name="chimeVolumeLabels">
<item>Use System Volume</item>
<item>Emad</item>
<item>Medium</item>
<item>Loud</item>
</string-array>
<string-array name="chimeVolumeValues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
This is the settings.xml with the listpreference:
<PreferenceCategory android:title="Media:">
<CheckBoxPreference android:key="ChimeWhenMusicIsPlaying"
android:title="@string/ChimeWhenMusicIsPlayingTitle" android:summary="@string/ChimeWhenMusicIsPlayingSummary"
android:defaultValue="false" />
<ListPreference android:title="Chime Volume"
android:key="ChimeVolume" android:summary="Select volume for the chiming sound."
android:entries="@array/chimeVolumeLabels" android:entryValues="@array/chimeVolumeValues"
android:defaultValue="1" />
</PreferenceCategory>
All help will be greatly appreciated.
Truly,
Emad
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 .equals 而不是 == 因为 == 比较引用而不是实际值,
例如
use .equals instead of == since == compare references not the actual value
like