如何更改Spinner文本颜色?

时间:2013-12-29 10:41:38

标签: android spinner

我在更改Spinner文字颜色方面遇到了问题。我使用以下代码更改文本颜色。但我不知道为什么它会NullPointerException setTextColor()。我正在动态地从数据库中获取微调器的项目。

 public class PersonalInformation extends Activity
 {

   @Override
   public void onCreate(Bundle savedInstanceState)
   {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.info)
    ArrayAdapter<String> adapter8 = new ArrayAdapter<String>(getApplicationContext(),
                        android.R.layout.simple_spinner_item,arrayList);
    adapter8.setDropDownViewResource(R.layout.spinner_view);
    mySpinner.setAdapter(adapter8);

    mySpinner.setOnItemSelectedListener(new OnItemSelectedListener()
    {
         @Override
         public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
         {

           ((TextView) arg0.getChildAt(arg2))
                            .setTextColor(Color.BLACK);
                    onulocation = arg0.getItemAtPosition(arg2)
                            .toString();
         }

        @Override
        public void onNothingSelected(AdapterView<?> parent)
        {
            // TODO Auto-generated method stub

        }
    });
}

1 个答案:

答案 0 :(得分:1)

这可能对你有帮助......

    mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {
            TextView textView = (TextView) view;
            textView.setTextColor(Color.BLACK);
            onulocation = parent.getItemAtPosition(pos).toString();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });