更改Spinner下拉列表选定项目的背景

时间:2014-04-10 05:55:59

标签: android background ontouchlistener onitemselectedlistener

我有一个微调器,需要在触摸(或按下)时更改背景图像。

我有样式设置,以便显示正确的图像,但我不能为我的生活得到背景图像来改变。

我的activity_main xml上的我的微调代码是:

<Spinner
    android:layout_width="220dp"
    android:layout_height="wrap_content"
    android:id="@+id/spinner"
    style="@style/SpinnerDropdown1"
    android:layout_marginTop="450dp"
    android:layout_centerHorizontal="true"
    android:entries="@array/counties"/>

这是我的@ style / SpinnerDropDown1代码:

<Resources>
  <style name="SpinnerDropdown1" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
    <item name="android:background">@drawable/unpressed</item>
    <item name="android:textColor">#ffffff</item>
    <item name="android:divider">#ffffff</item>
    <item name="android:dividerHeight">1dp</item>
  </style>
</Resources

,这是我的MainActivity.class

public class MainActivity extends ActionBarActivity implements OnItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

final Spinner spinner = (Spinner) findViewById(R.id.spinner);

     final ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.counties, R.layout.my_spinner_items);
    adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);


    spinner.setAdapter(adapter);

    spinner.setSelection(0);

    spinner.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            spinner.setBackgroundResource(R.drawable.pressed);
            return false;
        }
    });

    spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                   int pos, long id) {
            switch(pos) {

                case 1:
                    Intent j = new Intent (MainActivity.this, JasperOptions.class);
                    startActivity(j);
                    break;
                case 2:
                    Intent k = new Intent(MainActivity.this, LakeOptions.class);
                    startActivity(k);
                    break;
                case 3:
                    Intent l = new Intent(MainActivity.this, LaporteOptions.class);
                    startActivity(l);
                    break;
                case 4:
                    Intent n = new Intent(MainActivity.this, NewtonOptions.class);
                    startActivity(n);
                    break;
                case 5:
                    Intent p = new Intent(MainActivity.this, PorterOptions.class);
                    startActivity(p);
                    break;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {


        }

    });

    }

@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);
    return true;
  }


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    return id == R.id.action_settings || super.onOptionsItemSelected(item);
}


@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {

}

@Override
public void onNothingSelected(AdapterView<?> parentView) {

 }
}

0 个答案:

没有答案