无法在自定义微调器中选择带按钮的项目

时间:2014-09-08 05:55:14

标签: android button onclick spinner

我正在使用微调器项中的按钮和textview创建自定义微调器。 在这种情况下,仅检测按钮的单击。并且未检测到完整项目的点击。 但是当我从微调器项目中删除按钮时,单击完整项目工作正常。是否可以一次处理两个点击事件?如果是,我们如何实施呢?

代码如下: -

public class MainActivity extends Activity {

String[] Languages = { "Select a Language", "C# Language", "HTML Language",
        "XML Language", "PHP Language" };
// Declaring the Integer Array with resourse Id's of Images for the Spinners
Integer[] images = { 0, R.drawable.image_1, R.drawable.image_2,
        R.drawable.image_3, R.drawable.image_4 };

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

    // Declaring and typecasting a Spinner
    final Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);

    // Setting a Custom Adapter to the Spinner
    mySpinner.setAdapter(new MyAdapter(MainActivity.this, R.layout.custom,
            Languages));

}

// Creating an Adapter Class
public class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);

    }

    public View getCustomView(int position, View convertView,
            ViewGroup parent) {

        // Inflating the layout for the custom Spinner
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.custom, parent, false);

        // Declaring and Typecasting the textview in the inflated layout
        TextView tvLanguage = (TextView) layout
                .findViewById(R.id.tvLanguage);

        // Setting the text using the array
        tvLanguage.setText(Languages[position]);

        // Setting the color of the text
        tvLanguage.setTextColor(Color.rgb(75, 180, 225));

        // Declaring and Typecasting the imageView in the inflated layout
        Button img = (Button) layout.findViewById(R.id.imgLanguage);

        // Setting an image using the id's in the array
        img.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();
            }
        });

        int count = 0;
        // Setting Special atrributes for 1st element
        if (position == 0) {
            // Removing the image view
            img.setVisibility(View.GONE);
            // Setting the size of the text
            tvLanguage.setTextSize(20f);
            // Setting the text Color
            tvLanguage.setTextColor(Color.BLACK);

            count++;
            if(count ==0)
            tvLanguage.setVisibility(View.INVISIBLE);
        }

        return layout;
    }

    // It gets a View that displays in the drop down popup the data at the
    // specified position
    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {

        return getCustomView(position, convertView, parent);
    }

    // It gets a View that displays the data at the specified position
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        return getCustomView(position, convertView, parent);
    }

}

public class CustomSpinnerSelection extends Spinner {

    private boolean mToggleFlag = true;

    public CustomSpinnerSelection(Context context, AttributeSet attrs,
            int defStyle, int mode) {
        super(context, attrs);
    }

    public CustomSpinnerSelection(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomSpinnerSelection(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomSpinnerSelection(Context context, int mode) {
        super(context);
    }

    public CustomSpinnerSelection(Context context) {
        super(context);
    }

    @Override
    public int getSelectedItemPosition() {
        // this toggle is required because this method will get called in other
        // places too, the most important being called for the
        // OnItemSelectedListener
        if (!mToggleFlag) {
            return 0; // get us to the first element
        }
        return super.getSelectedItemPosition();
    }

    @Override
    public boolean performClick() {
        // this method shows the list of elements from which to select one.
        // we have to make the getSelectedItemPosition to return 0 so you can
        // fool the Spinner and let it think that the selected item is the first
        // element
        mToggleFlag = false;
        boolean result = super.performClick();
        mToggleFlag = true;
        return result;
    }

}

}

1 个答案:

答案 0 :(得分:4)

试试这个:

android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"