2

我有一个活动,它显示可以分配给我的应用程序中的活动事件的复选框(焦点区域)列表。但是,我希望能够阻止用户表单编辑这些值,直到单击按钮将表单置于编辑模式。

有人可以解释或提供代码示例如何将其实现到我的应用程序中吗?

此列表保存在 ListView 中,其中包含我为我的应用程序创建的类 FocusArea 的自定义 ArrayAdapter。Activity 的 xml 如下所示:

        <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:background="@drawable/horizontal_gradient_line_reverse">

    <TextView
        android:id="@+id/textViewCustomerNameNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Customer Name &amp; Number" android:layout_margin="5dp"/>

    <ListView
        android:id="@+id/listViewFocusAreas"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/relativeLayoutControls"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textViewCustomerNameNumber"
        android:layout_margin="5dp"
        android:background="@drawable/myborder" >

    </ListView>

    <RelativeLayout
        android:id="@+id/relativeLayoutControls"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" >


        <ImageView
            android:id="@+id/imageViewCancelFocusAreaChanges"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/ic_mini_undo" />

        <ImageView
            android:id="@+id/imageViewSaveFocusAreaChanges"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/imageViewCancelFocusAreaChanges"
            android:src="@drawable/ic_mini_save" />

        <ImageView
            android:id="@+id/imageViewEditFocusArea"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/imageViewSaveFocusAreaChanges"
            android:src="@drawable/ic_mini_edit" />

    </RelativeLayout>


    <ImageView
        android:id="@+id/imageViewEditMode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textViewCustomerNameNumber"
        android:layout_alignRight="@+id/listViewFocusAreas"
        android:src="@drawable/edit16x16"
        android:visibility="gone" />

</RelativeLayout>

以下是焦点区域行的 xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <CheckBox
        android:id="@+id/checkBoxFocusArea"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Focus Area" />

</LinearLayout>

以下是自定义 ArrayAdapter 的代码:

    public class FocusAreaAdapter extends ArrayAdapter<FocusArea> {

    private ArrayList<FocusArea> focusAreas;
    private Context context;

    public FocusAreaAdapter(Context context, int textViewResourceId, ArrayList<FocusArea> focusAreas) {
        super(context, textViewResourceId, focusAreas);
        this.context = context;
        this.focusAreas = focusAreas;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        View v = convertView;
        if(v == null) {
            LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.focus_area_menu_item, null);
        }
        FocusArea focusArea = focusAreas.get(position);

        if(focusArea != null) {
            CheckBox cbFocusArea = (CheckBox)v.findViewById(R.id.checkBoxFocusArea);
            cbFocusArea.setText(focusArea.getFocusAreaCodeDescription());
            if(focusArea.isFocusAreaCodeChecked() == 1) {
                cbFocusArea.setChecked(true);
            } else {
                cbFocusArea.setChecked(false);
            }

            cbFocusArea.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    FocusArea focusArea = focusAreas.get(position);
                    if(focusArea.isFocusAreaCodeChecked() == 1) {
                        focusArea.setFocusAreaCodeChecked(0);
                    } else {
                        focusArea.setFocusAreaCodeChecked(1);
                    }
                }
            });
        }       
        return v;
    }
}

最后,我使用以下代码实例化了我的自定义数组适配器:

    private void assignFocusAreasToAdapter() {
    // get reference to available responsibles list view control
    lvFocusAreas.clearChoices();

    // create an array adapter with the communications data array
    activityFocusAreaAdapter = new FocusAreaAdapter(this, R.layout.focus_area_menu_item, activityFocusAreas);

    // assign the my customers list control the array adapter
    lvFocusAreas.setAdapter(activityFocusAreaAdapter);
    activityFocusAreaAdapter.notifyDataSetChanged();
    Log.i(TAG, "Available Activity Focus Areas Loaded");
}
4

1 回答 1

2

好的,我为可能遇到相同问题的任何人找到了解决此问题的方法。

焦点区域活动的 XML 与上面发布的相同。

焦点区域行项目的 XML 进行了相应的修改:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants" >

    <CheckBox
        android:id="@+id/checkBoxFocusArea"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Focus Area" />

</LinearLayout>

您可以看到现在添加了以下行android:descendantFocusability="blocksDescendants"这可以防止复选框 onClickListener 触发,这通常会阻止 ListViews OnItemClickedListener 触发。

修改自定义适配器代码以删除复选框 OnClickListener,如下所示:

public class FocusAreaAdapter extends ArrayAdapter<FocusArea> {

    private ArrayList<FocusArea> focusAreas;
    private Context context;

    public FocusAreaAdapter(Context context, int textViewResourceId, ArrayList<FocusArea> focusAreas) {
        super(context, textViewResourceId, focusAreas);
        this.context = context;
        this.focusAreas = focusAreas;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        View v = convertView;
        if(v == null) {
            LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.focus_area_menu_item, null);
        }
        FocusArea focusArea = focusAreas.get(position);

        if(focusArea != null) {
            CheckBox cbFocusArea = (CheckBox)v.findViewById(R.id.checkBoxFocusArea);
            cbFocusArea.setText(focusArea.getFocusAreaCodeDescription());
            if(focusArea.isFocusAreaCodeChecked() == 1) {
                cbFocusArea.setChecked(true);
            } else {
                cbFocusArea.setChecked(false);
            }

            cbFocusArea.setClickable(false);            
        }       
        return v;
    }
}

自定义适配器使用与上面发布的相同代码进行实例化,但您必须将 OnItemClickListener 添加到 ListView 并执行 OnItemClickListener click 事件中的复选框的检查,如下所示:

    lvFocusAreas = (ListView)findViewById(R.id.listViewFocusAreas);
    lvFocusAreas.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
            if(editMode){
                FocusArea focusArea = (FocusArea)lvFocusAreas.getItemAtPosition(position);
                if(focusArea.isFocusAreaCodeChecked() == 0) {
                    focusArea.setFocusAreaCodeChecked(1);
                } else {
                    focusArea.setFocusAreaCodeChecked(0);
                }
                activityFocusAreaAdapter.notifyDataSetChanged();
                setControlsEnabled();
            }               
        }
    });

希望这可以帮助任何有同样问题的人。:D

于 2012-06-01T10:22:28.060 回答