如何禁用&在android中的自定义列表视图中启用单选按钮

时间:2013-04-24 07:40:50

标签: android android-listview

![在此输入图片说明] [1]我自定义ListView RadioButton&每行Text。我怎么能禁用&启用RadioButton中的ListView次点击(即)。
我怎么能禁用&允许通过ENABLE&选择ListView RadioButton s。禁用Button次点击。
以下是我的代码。 我该怎么办?

ListView LSOne;
        Button lock, unlock;
        int[] _intRadio = new int[20];
        String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
                        "Jupiter", "Saturn", "Uranus", "Neptune" };

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                LSOne = (ListView) findViewById(R.id.listView1);
                lock = (Button) findViewById(R.id.lock);
                unlock = (Button) findViewById(R.id.unlock);
                lock.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            **LsAdapter.isEnabled = !LsAdapter.isEnabled;
            int first = LSOne.getFirstVisiblePosition();
            for (int i = first; LSOne.getChildAt(i) != null; i++) {
                RadioButton button = ((RadioButton) LSOne.getChildAt(i)
                        .findViewById(R.id.radioButton1));
                button.setEnabled(LsAdapter.isEnabled);**

                        }
                });

                unlock.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                                //need to do enable option

                        }
                });

                ArrayList<String> planetList = new ArrayList<String>();
                planetList.addAll(Arrays.asList(planets));

                // Create ArrayAdapter using the planet list.
                LsAdapter listAdapter = new LsAdapter(this, R.layout.country_info,
                                planetList);

                LSOne.setAdapter(listAdapter);

        }

        public class LsAdapter extends ArrayAdapter<String> {
                private LayoutInflater mInflater;
                private String[] mTaxi = null;
                private String[] mid = null;
                long id;
                **public boolean isEnabled;**
                private int mViewResourceId;

                public LsAdapter(Context ctx, int viewResourceId,
                                ArrayList<String> planetList) {
                        super(ctx, viewResourceId);

                        mInflater = (LayoutInflater) ctx
                                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                        String[] tax = planetList.toArray(new String[planetList.size()]);

                        mTaxi = tax;

                        mViewResourceId = viewResourceId;
                }

                @Override
                public int getCount() {
                        return mTaxi.length;
                }

                @Override
                public String getItem(int position) {
                        return mTaxi[position];
                }

                @Override
                public long getItemId(int position) {
                        return 0;
                }

                @Override
                public int getViewTypeCount() {
                        // TODO Auto-generated method stub
                        return 20;
                }

                @Override
                public int getItemViewType(int position) {
                        // TODO Auto-generated method stub
                        return position;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                        ViewHolder holder = null;
                        Log.v("ConvertView", String.valueOf(position));
                        int _intPosition = getItemViewType(position);
                        if (convertView == null) {
                                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                convertView = vi.inflate(R.layout.country_info, null);
                                holder = new ViewHolder();
                                holder.code = (TextView) convertView
                                                .findViewById(R.id.textView1);
                                holder.name = (RadioButton) convertView
                                                .findViewById(R.id.radioButton1);
                                convertView.setTag(holder);

                                holder.code.setText(mTaxi[position]);
                                holder.name.setId(_intPosition);
                        **if (isEnabled) {
                holder.name.setEnabled(true);
            } else if (!isEnabled) {
                holder.name.setEnabled(false);
            }**

                                holder.name.setOnClickListener(new View.OnClickListener() {
                                        public void onClick(View v) {
                                                for (int i = 0; i < _intRadio.length; i++) {
                                                        if (i == v.getId()) {
                                                                _intRadio[i] = 1;
                                                        } else {
                                                                _intRadio[i] = 0;
                                                        }
                                                }
                                                notifyDataSetChanged();
                                        }
                                });
                        } else {
                                holder = (ViewHolder) convertView.getTag();
                        }

                        if (_intRadio[_intPosition] == 1) {
                                holder.name.setChecked(true);
                        } else {
                                holder.name.setChecked(false);
                        }

                        return convertView;

                }

                private class ViewHolder {
                        TextView code;
                        RadioButton name;
                        Button btn;
                }
        }

感谢。

2 个答案:

答案 0 :(得分:2)

您不必编写两种不同的锁定/解锁实现。您可以在LsAdapter中创建一个公共布尔变量,以便LSOne可以切换它:

public class LsAdapter extends ArrayAdapter<String> {
    ...
    public boolean isEnabled;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    ...
    if(isEnabled)
    //enable or disable the radio button here

}

然后,创建一个像这样的onClickListener:

lock = (Button) findViewById(R.id.lock);
lock.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        LsAdapter.isEnabled = !LsAdapter.isEnabled;
        int first = LSOne.getFirstVisiblePostition();
        for(int i=first; LSOne.getChildAt(i)!=null; i++) {
            RadioButton button = ((RadioButton) LSOne.getChildAt(i).findViewById(your_radio_button_id));
            button.setEnabled(LsAdapter.isEnabled);
        }
    }
});

答案 1 :(得分:1)

btn.setEnabled(false); //to disable

btn.setEnabled(true); //to enable

有时它不会停止在某些设备上进行多/快速点击,因此备选选项是

btn.setClickable(false); //to disable

btn.setClickable(true); //to enable