android - 无法使用自定义微调器中的按钮选择项目

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

}

}

最佳答案

试试这个:

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

关于android - 无法使用自定义微调器中的按钮选择项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25718148/

相关文章:

c# - 将文本设置为剪贴板 Xamarin

javascript - 如何让 jQuery 对话框按钮扩展到对话框之外并彼此相邻显示?

python - 将函数绑定(bind)到 Kivy 按钮

jquery - 在 jQuery 中嵌套点击事件

java - 4个错误-: package com.google.android.gms.appstate ETC错误

启用了 gpu 仿真的 android 模拟器得到了奇怪的屏幕

android - 无法使用 npm install -g 安装 turtle-cli

java - 如何使按钮随机移动

javascript - 显示隐藏的 Div 在提交时保持隐藏状态

javascript - Bootstrap 面板折叠 onclick 事件和字形