定制RadioButton在前的Spinner

来源:互联网 发布:生命游戏 java代码 编辑:程序博客网 时间:2024/05/17 06:13

效果图:a

代码:

1.。。drawable下定义radio_bg。xml

<?xml version="1.0" encoding="utf-8"?>
<selector
 xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_checked="true"
           android:drawable="@drawable/radio_checked"/> <!-- pressed -->
     <item android:state_focused="true"
           android:drawable="@drawable/radio_checked"/> <!-- focused -->
     <item android:drawable="@drawable/radio"/> <!-- default -->
</selector>
2===layout下定义spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/spinner_item_textview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:singleLine="true"
  android:textColor="@color/red"
  android:textSize="24sp">
</TextView>
3===layout下定义spinner_dropdown_item.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="40dp"
  android:id = "@+id/spinner_drop_item_view"
  android:singleLine="true"
  android:textColor="@color/green"
  android:textSize="24sp"
  style="?android:attr/spinnerDropDownItemStyle"
  android:drawableLeft="@drawable/radio_bg"
  android:checkMark="@null">
  <!-- if the RadioButton put behind text -->
 <!-- android:checkMark="@drawable/radio_bg" -->

</CheckedTextView>

4===MainActivity.java

package com.android.tian;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {
 private View testView;
 private Spinner mySpinner;

 //define an array[] to save "cities"
 private static final String[] countriesStr = {"Bejing",
  "shanghai","tianjin","DaLian"};
 private ArrayAdapter<String> adapter;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mySpinner = (Spinner)findViewById(R.id.spinner_City);

        //simple_spinner_item || simplle_spinner_dropdown_item
        adapter = new ArrayAdapter<String>(this,R.layout.spinner_item,countriesStr);
       
        //set the item style
        adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
       // adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       
        //add the content in the adapter into the spinner
        mySpinner.setAdapter(adapter);
       
        //Selected Event  
        mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
   @Override
   public void onItemSelected(AdapterView<?> arg0, View arg1,
     int pos, long arg3) {
    // TODO Auto-generated method stub
    //arg0==spinner,arg1==TextView;This TextView is the one
    //included in the Spinner!!!!
    testView = arg1;   
   }
   @Override
   public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
   }
   
        });
        //Touch Screen Event
        mySpinner.setOnTouchListener(new Spinner.OnTouchListener(){
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    //Execute animation
    //v==spinner
    //v.startAnimation(myAnimation);
    //v.setVisibility(View.INVISIBLE);
    return false;
   }
        });
        //Focus changing
        mySpinner.setOnFocusChangeListener(new Spinner.OnFocusChangeListener(){
   @Override
   public void onFocusChange(View v, boolean hasFocused) {
    // TODO Auto-generated method stub
    
   }     
        });
    }

}