2016-11-14 7 views
0

私はAndroidアプリで特定の色をすばやく選択しようとしています。リストのオプションを簡単なダイアログで設定することができます色。それは簡単だと思われますが、私は直接の答えを見つけることができませんでした。AndroidのAlertDialogで個々の項目の背景色をプログラムで変更します

どのようにして(実行時にプログラムで)各オプションの背景色を設定できますか?

+0

ポストあなたがこれまでに – ik024

+0

を試してみましたコードあなたが構築する必要がありますカスタムダイアログに@Ramesh投稿が表示されます –

答えて

0

返信いただきありがとうございます。

public class ColourItem { 
    private String displayString; 
    private int colour; 

    public ColourItem (int colour) { 
     this(colour, ""); 
    } 
    public ColourItem (int colour, String displayString) { 
     this.colour = colour; 
     this.displayString = displayString; 
    } 

    @Override 
    public String toString() { 
     return displayString; 
    } 
    public void setDisplayString(String s){ 
     this.displayString = s; 
    } 

    public int getColour(){ 
     return colour; 
    } 
    public void setColour(int i){ 
     this.colour = i; 
    } 
} 

答えが触発された:私は

private void showColourChooserDialog(){ 
    final ColourItem[] items = { 
      new ColourItem(Color.RED), 
      new ColourItem(Color.GREEN), 
      new ColourItem(Color.BLUE), 
    };  

    ListAdapter adapter = new ArrayAdapter<ColourItem>(
      this, 
      android.R.layout.select_dialog_item, 
      android.R.id.text1, 
      items){ 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View v = super.getView(position, convertView, parent); 
      TextView tv = (TextView)v.findViewById(android.R.id.text1); 

      int colour = items[position].getColour(); 
      tv.setBackgroundColor(colour); 

      return v; 
     } 
    }; 

    new AlertDialog.Builder(this) 
    .setTitle("Choose Colour") 
    .setAdapter(adapter, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      // ... respond to choice here 
     } 
    }).show(); 
} 

ColourItemクラスが...それを考え出したThis solution regarding custom icons for dialog items

0

私はこれが役に立つかもしれないと思う:

private void showAlertDialogForColors() { 
     final AlertDialog.Builder alert = new AlertDialog.Builder(this); 
     LayoutInflater inflater = this.getLayoutInflater(); 
     View alertLayout = inflater.inflate(R.layout.choose_color_theme, null); 
     //alert.setTitle("Theme Color"); 
     // this is set the view from XML inside AlertDialog 
     alert.setView(alertLayout); 
     final Integer tempValue = SessionManager.getInstance(ColorSettingsActivity.this).getPrefThemeCode(); 

     TextView textViewBlue = (TextView) alertLayout.findViewById(R.id.themeColorBlue); 
     TextView textViewOrange = (TextView) alertLayout.findViewById(R.id.themeColorOrange); 
     TextView textViewPink = (TextView) alertLayout.findViewById(R.id.themeColorPink); 
     TextView textViewPurple = (TextView) alertLayout.findViewById(R.id.themeColorPurple); 
     TextView textViewCyan = (TextView) alertLayout.findViewById(R.id.themeColorCyan); 
     TextView textViewDarkGreen = (TextView) alertLayout.findViewById(R.id.themeColorDarkGreen); 
     selectedThemeColor = (TextView) alertLayout.findViewById(R.id.textViewSelectedTheme); 
     selectedThemeColor.setTypeface(font); 

     textViewBlue.setOnClickListener(this); 
     textViewOrange.setOnClickListener(this); 
     textViewPink.setOnClickListener(this); 
     textViewPurple.setOnClickListener(this); 
     textViewCyan.setOnClickListener(this); 
     textViewDarkGreen.setOnClickListener(this); 


     // disallow cancel of AlertDialog on click of back button and outside touch 
     alert.setCancelable(false); 
     alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       SessionManager.getInstance(MessageSettingsActivity.this).setPrefThemeCode(tempValue); 
       Toast.makeText(getBaseContext(), "Cancel clicked", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       recreate(); 
      } 
     }); 
     AlertDialog dialog = alert.create(); 
     dialog.show(); 
    } 

あなたが事前に定義された色のセットを持っている場合は、これを使用するか、動的にShared PreferencesまたはDBに保存されている色を渡し、その後リスト

に移入したいですあなたが私のケースで特定の項目をクリックするたびに、5つの定数があります。

関連する問題