所以我正在制作我的第二个应用程序,我决定构建一个具有多个皮肤/图标集的菜单,可以随机化主菜单按钮,我让一切正常,然后我开始开发用户首次启动时启动的简介屏幕应用程序使用对话框,现在我在用户选择不同方案时动态更改颜色,工作正常,当用户来到框时询问他们是否要每次都随机化菜单按钮的颜色:
如果我点击Randomize它会动态改变,但是如果我按顺序切换那么它根本不会改变,我不明白。
我有3个主要文件: ConversePrefs.java,我不会发布它,因为我100%确定它是正确的,因为它只是一组获取共享首选项的快捷方式函数。
MainMenu.java - 主要活动/主菜单:
package com.arl.conversion;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainMenu extends Activity {
private static final OnClickListener PreSetupsColor = null;
final Context context = this;
ColorCollections cc = new ColorCollections(context);
List<String> colorList = null;
int currentIndex = 0;
public void LoadColorPalette(){
ConversePrefs cp = new ConversePrefs(this);
colorList = cc.GetColorList(cp.GetIntSetting("UsersColor", 0),cp.GetBolSetting("UsersRandomizer", false));
}
public void SetBtnColour(int btn){
Button bttn = (Button)findViewById(btn);
bttn.setBackgroundColor(Color.parseColor(colorList.get(currentIndex)));
currentIndex++;
}
public void GenerateButtonsDefault(){
ConversePrefs cp = new ConversePrefs(this);
LoadColorPalette();
SetBtnColour(R.id.DistanceBtn);
SetBtnColour(R.id.VolumeBtn);
SetBtnColour(R.id.AreaBtn);
SetBtnColour(R.id.ClothingBtn);
SetBtnColour(R.id.ElectricBtn);
SetBtnColour(R.id.TempBtn);
SetBtnColour(R.id.TimeBtn);
SetBtnColour(R.id.MoneyBtn);
SetBtnColour(R.id.SpeedBtn);
SetBtnColour(R.id.MathBtn);
SetBtnColour(R.id.CurrencyBtn);
SetBtnColour(R.id.OptionsBtn);
}
public void RefreshMenu(){
currentIndex = 0;
GenerateButtonsDefault();
}
//SECOND
public void PreSetuposColor(final View v){
final ConversePrefs cp = new ConversePrefs(this);
String[] ColorSchemes = {"Windows 8", "Modern Urban"};
new AlertDialog.Builder(this).
setTitle("Color Scheme").
setSingleChoiceItems(ColorSchemes, cp.GetIntSetting("UsersColor", 0), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
cp.SetIntSetting("UsersColor", which);
RefreshMenu();
}
}).
setPositiveButton("Continue", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
MainMenu.this.PreSetupRandoms(v);
}
}).setNeutralButton("Back", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
MainMenu.this.PreSetupsWelcome(v);
}
}).setNegativeButton("Quit", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
System.exit(0);
}
}).setCancelable(false).show();
}
// THIRD
public void PreSetupRandoms(final View v){
final ConversePrefs cp = new ConversePrefs(this);
String[] FalseAndTrue = {"Sequential", "Randomize"};
new AlertDialog.Builder(this).
setTitle("Randomize Colors?").
setSingleChoiceItems(FalseAndTrue, cp.GetBoolInt(cp.GetBolSetting("UsersRandomizer", false)), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
cp.SetBooleanSetting("UsersRandomizer", cp.GetIntBool(which));
RefreshMenu();
}
}).setPositiveButton("Continue", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO
}
}).setNeutralButton("Back", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
MainMenu.this.PreSetuposColor(v);
}
}).setNegativeButton("Quit", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
System.exit(0);
}
}).setCancelable(false).show();
}
//FIRST
public void PreSetupsWelcome(final View v){
new AlertDialog.Builder(this).
setTitle("Welcome").
setMessage("It seems that this is the first time application was ran on this device, you will be now presented with app appearance setup of your own choice.").
setPositiveButton("Continue", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
MainMenu.this.PreSetuposColor(v);
}
}).
setNegativeButton("Quit", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
System.exit(0);
}
}).
setCancelable(false).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ConversePrefs cp = new ConversePrefs(this);
//LoadColorPalette();
setContentView(cc.GetTheme(cp.GetIntSetting("ThemeName", 0)));
GenerateButtonsDefault();
PreSetupsWelcome(new View(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
}
ColorCollections.java - 包含颜色信息和少量检索功能的文件。
package com.arl.conversion;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
public class ColorCollections{
Context context;
public ColorCollections(Context context){
this.context = context;
}
//Color Palettes.
public static String[] Win8 = new String[]{"#008299","#00A0B1","#2672EC","#2E8DEF","#8C0095","#A700AE","#5133AB","#643EBF","#AC193D","#BF1E4B","#D24726","#DC572E","#008A00","#00A600","#094AB2","#0A5BC4"};
public static String[] ModernUrban = new String[]{"#47B6BE","#73CC3E","#FF7149","#FF5063","#FF6999","#233977","#4B9364","#8A8F1D","#A63B52","#B23265","#883E57","#888788","#87A087"};
public List<String> GetColorList(int colorindex, boolean randomizer){
List<String> colorList = Arrays.asList(Win8);
switch(colorindex){
case 0:
colorList = Arrays.asList(Win8);
break;
case 1:
colorList = Arrays.asList(ModernUrban);
break;
}
if(randomizer == true){
Collections.shuffle(colorList);
Collections.shuffle(colorList);
Collections.shuffle(colorList);
Collections.shuffle(colorList);
}
return colorList;
}
public int GetTheme(int theme){
int themeID = 0;
switch(theme){
case 0:
themeID = R.layout.activity_main_menu_light;
break;
case 1:
themeID = R.layout.activity_main_menu_light_bg;
break;
case 2:
themeID = R.layout.activity_main_menu_dark;
break;
case 3:
themeID = R.layout.activity_main_menu_dark_bg;
break;
}
return themeID;
}
}
有人可以找出问题吗?过去2个小时我一直坚持这一点,尝试了一切,甚至最终得到了更高效的代码(我知道这对你来说可能看起来很糟糕,但更糟糕的是,相信我)。
此外,我有4组图标,每组12个图标,每组都有图标的前缀,它是一个实际导入的drawable .png。是否有更好的方法来创建它们的XML列表而不是手动输入?
谢谢。
答案 0 :(得分:0)
我找不到问题的确切位置,但如果你没有找到调试将是有益的,并进入setSingleChoiceItems(...)作为开始的函数。您可以使用ListView显示XML列表。以下是从strings.xml文件生成的示例:http://www.androidhive.info/2011/10/android-listview-tutorial/。更复杂的ListViews将要求您创建自定义适配器类。