【问题标题】:Unable to select item with button in custom spinner无法在自定义微调器中选择带有按钮的项目
【发布时间】:2014-09-08 15:05:36
【问题描述】:

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

代码如下:-

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;
    }

}

}

【问题讨论】:

  • 是的 OnitemSelectListner For Spinner 在 Activity 上使用它

标签: android button onclick spinner


【解决方案1】:

试试这个:

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

【讨论】:

  • 完美。 But one problem arises, when the item with button is selected, then the spinner sets the button also on the spinner alongwith the text of the item.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-15
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多