为Android中的多个CustomAutoCompleteTextView实现setOnFocusChangeListener

时间:2018-01-27 04:44:38

标签: java android xml

List will added like this to First CustomAutoCompleteTextView

List will added like this to second CustomAutoCompleteTextView

我将列表设置为第一个textview,当用户将焦点更改为第二个textview时,我需要检查用户是否从列表中选择条目,否则他从键盘手动输入。如果他手动输入,那么我想将该文本视图设为null

同样,另一个列表设置为第二个textview,当焦点更改为第三个textview时,我需要检查用户从列表中选择条目,否则他从键盘手动输入。如果他手动输入,我想将该文本视图设为null

同样,另一个列表设置为第三个textview,当焦点从第三个textview更改时,应该执行相同的操作。

第一个textview工作正常意味着当我点击该方法正在调用的第二个文本视图时,如果用户手动输入,则将该字段设为null。

public class Entry_Fragment extends Fragment implements View.OnFocusChangeListener, View.OnClickListener {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.entry_fab_activity, container, false);
        try {
            actvEntryCategory = (CustomAutoCompleteTextView) rootView.findViewById(R.id.actventry);
            actvPType = (CustomAutoCompleteTextView) rootView.findViewById(R.id.actvProductType);
            actvDivName = (CustomAutoCompleteTextView) rootView.findViewById(R.id.actventryDivName);
            mContext = (EEmployeeApplication) rootView.getContext().getApplicationContext();
            WorkTypes = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.WorkTypes)));

            actvEntryCategory.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    String str = actvEntryCategory.getText().toString();
                    if(b)
                    {
                        actvDivName.setText("");
                        actvPType.setText("");
                    }
                    else
                    {
                        for (int i=0;i<EntryTypeAdapter.getCount();i++)
                        {
                            String temp = EntryTypeAdapter.getItem(i).toString();
                            if (str.compareTo(temp) == 0){
                                return;
                            }
                        }
                        actvEntryCategory.setText("");
                    }

                   // actvEntryCategory.setError("Please select valid Entry Type");
                }
            });

            // Get Available Work types from Database
            try {
                WorksList = dbHandler.getWorkTypes();
                for (String newWorkType : WorksList) {
                    if (!WorkTypes.contains(newWorkType)) {
                        WorkTypes.add(newWorkType);
                    }
                }

            } catch (Exception getWorkExp) {
                WorksList = new ArrayList<String>();
                Log.d("eEMP/EntryCreate", "Error raised at getting Work Types list due to " + getWorkExp.toString());
            }

            EntryTypeAdapter = new CustomArrayAdapter(getActivity(), R.layout.autocomplete_text_layout, WorkTypes);
            actvEntryCategory.setAdapter(EntryTypeAdapter);
            actvEntryCategory.setThreshold(0);
            actvDivName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                   try{
                       String str = actvDivName.getText().toString();
                       if(b){
                           actvPType.setText("");
                       }

                       else{
                           for (int i=0;i<QRDivArrayAdapter.getCount();i++)
                           {
                               String temp = QRDivArrayAdapter.getItem(i).toString();
                               if (str.compareTo(temp) == 0){
                                   return;
                               }
                           }
                           actvDivName.setText("");
                       }
                }catch(Exception focusfordivision)
                   {
                       Log.d("eEmp/Resume", "Error raised due to " + focusfordivision.toString());
                   }
            }
            });
            actvPType.setOnFocusChangeListener(new View.OnFocusChangeListener(){
                @Override
                public void onFocusChange(View v, boolean b) {
                    try{
                        String str = actvDivName.getText().toString();
                        if(b){
                            actvPType.setText("");
                        }
                        else{
                            for (int i=0;i<QRDivArrayAdapter.getCount();i++)
                            {
                                String temp = QRDivArrayAdapter.getItem(i).toString();
                                if (str.compareTo(temp) == 0){
                                    return;
                                }
                            }
                            actvDivName.setText("");
                        }
                    }catch(Exception focusfordivision)
                    {
                        Log.d("eEmp/Resume", "Error raised due to " + focusfordivision.toString());
                    }
                }
            });
            Rlys = new ArrayList<Rly>();

            try {

                // DivList = dbHandler.getDivisions();
                DivStnList = mContext.StnsPlacesList;


                for (int i = 0;i < DivStnList.size();i++){
                    QRDivTextItemList = mContext.StnsPlacesList;
                    DivIDsList.add(DivStnList.get(i).did);
                    StnIDsList.add(DivStnList.get(i).id);
                    DivList.add(DivStnList.get(i).subItem2Name);
                    StnsList.add(DivStnList.get(i).headItemName);
                }
            } catch (Exception getDivExp) {
                DivList = new ArrayList<String>();
                Log.d("eEMP/EntryCreate", "Error raised at getting Divisions list due to " + getDivExp.toString());
            }

            actvDivName.setThreshold(0);
            actvDivName.addTextChangedListener(new AutoCompleteTextWatcher(actvDivName));
            actvDivName.setOnItemClickListener(new AutoCompleteItemClickListener<TextItem>(QRDivTextItemList,actvDivName));
            QRDivArrayAdapter = new CustomAdapter(mContext,R.layout.autocomplete_text_view,QRDivTextItemList);
            actvDivName.setAdapter(QRDivArrayAdapter);
            actvDivName.setCustomAutoCompleteTextViewListener(new CustomAutoCompleteTextView.CustomAutoCompleteTextViewListener() {
                @Override
                public void onItemSelected() {
                    try {
                        TextItem selectedItem = (TextItem) actvDivName.getTag();
                        actvPType.setText("");
                        ProdsItemList.clear();
                        actvDivName.setVisibility(View.VISIBLE);

                        if (selectedItem != null ){
                            instProd = (List<InstProd>) selectedItem.childObject;
                            for (int i=0;i<instProd.size();i++){
                                TextItem item = new TextItem();
                                item.headItemName = instProd.get(i).IName;
                                //item.subItem1Name = instProd.get(i).MName;
                                item.subItem1Name = String.valueOf(instProd.get(i).PID);
                                item.subItem2Name = String.valueOf(instProd.get(i).IID);
                                ProdsItemList.add(item);
                            }
                        }
                        QRProdArrayAdapter.setTextObjects(ProdsItemList);

                    }catch (Exception e){
                        Log.d("eEmp/TextItemQR", "Exception occurred due to " + e.toString());
                    }
                }
            });
            QRProdArrayAdapter = new CustomAdapter(mContext,R.layout.autocomplete_text_view,ProdsItemList);//added by pavani
            actvPType.setAdapter(QRProdArrayAdapter);//added by pavani

            actvPType.setThreshold(0);
            actvPType.addTextChangedListener(new AutoCompleteTextWatcher(actvPType));
            actvPType.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    try {
                        val = position;
                        iid =Integer.parseInt(ProdsItemList.get(position).subItem2Name) ;
                        pid=Integer.parseInt(ProdsItemList.get(position).subItem1Name);
                        station = ProdsItemList.get(position).headItemName;
                        product = ProdsItemList.get(position).subItem1Name;
                    }catch (Exception e){
                        Log.d("eEmp/OnProdClick", "Exception Occurred due to " + e.toString());
                    }

                    //sel = (String)parent.getItemAtPosition(position);
                }
            });
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // Disallow the touch request for parent scroll on touch of child view
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    fab_menu.close(true);
                    return false;
                }
            });
            if (mContext != null) {

                // Check User Authentication
                CheckUserCredentialsTask UserValTask = new CheckUserCredentialsTask();
                UserValTask.setContextandActivity(mContext, getActivity());
                UserValTask.execute();
            }
        } catch (Exception expt) {
            Log.d("eEmp/EntryCreate", "Exception Occurred due to " + expt.toString());
        }

        return rootView;
    } 
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        fab_menu.close(true);
    }

    // Save Instance of this fragment added in Ver 1.2 by JVK & Shamili
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        try {
            outState.putParcelable(EmpConstants.SavedEntryInstanceKey, getFragmentData());
            //Log.d("eEmp/EntrySaveInst", "Saved Instance Created");
        } catch (Exception EntrySaveInstanceExp) {
            Log.d("eEmp/EntrySaveInstExp", "Error raised due to " + EntrySaveInstanceExp.toString());
        }
    }
    //added in Ver 1.2 by JVK & Shamili
    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        if (savedInstanceState != null) {
            // Log.d("eEmp/EntryRestSaveInst", "Saved Instance is not Null");
            readFromParcel(savedInstanceState);
        } else {
            //Log.d("eEmp/EntryRestSaveInst", "Saved Instance is Null");
        }
    }

    // Assign SavedInstance Values to Controls added in Ver 1.2 by JVK & Shamili
    private void readFromParcel(Bundle inState) {
        try {
            SaveEntryInstance inMembers = (SaveEntryInstance) inState.get(EmpConstants.SavedEntryInstanceKey);
            if (inMembers != null) {
                actvEntryCategory.setText(inMembers.EntryType);
                actvDivName.setText(inMembers.Division);
                actvPType.setText(inMembers.ProjectName);
                edtFromDate.setText(inMembers.FromDate);
                edtFromTime.setText(inMembers.FromTime);
                edtToDate.setText(inMembers.ToDate);
                edtToTime.setText(inMembers.ToTime);
                edtNumber1.setText(inMembers.Num1);
                edtNumber2.setText(inMembers.Num2);
                edtTF1.setText(inMembers.TF1);
                edtTF2.setText(inMembers.TF2);
                edtTF3.setText(inMembers.TF3);
                edtTF4.setText(inMembers.TF4);
                edtTF5.setText(inMembers.TF5);
                listOfImages = inMembers.listOfImgs;
                WorkTypes = inMembers.WorksList; // Added in V 1.3
                DivList = inMembers.DivisionsList; // Added in V 1.3

                ImgTakenDateTImeList = inMembers.ImgTakenDateTImeList;

                // TODO: 28/09/2017 ImgTakenDateTImeList Need to add to Adapter

                ImgListAdapter = new ImgAdapter(mContext, R.layout.display_image_item, listOfImages, entryFragment);
                imageListView.setAdapter(ImgListAdapter);

                // Added in V 1.3
                EntryTypeAdapter = new CustomArrayAdapter(getActivity(), R.layout.autocomplete_text_layout, WorkTypes);
                actvEntryCategory.setAdapter(EntryTypeAdapter);

                // Added in V 1.3
                DivAdapter = new CustomArrayAdapter(getActivity(), R.layout.autocomplete_text_layout, DivList);
                actvDivName.setAdapter(DivAdapter);

                // Log.d("eEmp/EntryRestSaveInst", "Restoring Saved Instance is Completed");
            }
        } catch (Exception entryParcelReadExp) {
            Log.d("eEmp/EntryParcelReadExp", "Error raised due to " + entryParcelReadExp.toString());
        }
    }

    @Override
    public void onDestroy() {
        //Log.d("eEmp/EntryFragment", "onDestroy Called");
        super.onDestroy();
    } 
}
   
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">

<LinearLayout
    android:id="@+id/LLPlaceDetails"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    android:weightSum="1">

    <android.support.v7.widget.CardView
        android:id="@+id/zone_card_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="@dimen/activity_horizontal_margin"
        android:layout_marginLeft="@dimen/activity_custom_margin"
        android:layout_marginRight="@dimen/activity_custom_margin"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:layout_weight="0.54"
        card_view:cardElevation="@dimen/cardview_default_elevation"
        card_view:contentPadding="10dp">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:focusableInTouchMode="false">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_layout_zone"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:layout_weight="1">

                    <com.efftronics.android.eEmployee.Common.UI.CustomAutoCompleteTextView
                        android:id="@+id/actventry"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:ems="11"
                        android:hint="@string/Entry_Type"
                        android:inputType="textCapWords|textMultiLine"
                        android:maxLength="250"
                        android:theme="@style/AutocompleteTheme" />
                </android.support.design.widget.TextInputLayout>
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_layout_Division"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:layout_weight="1">

                    <com.efftronics.android.eEmployee.Common.UI.CustomAutoCompleteTextView
                        android:id="@+id/actventryDivName"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:hint="@string/station"
                        android:inputType="textCapWords|textMultiLine"
                        android:maxLength="250"
                        app:hintTextAppearance="@style/AutocompleteTheme"
                         />
                </android.support.design.widget.TextInputLayout>
            </TableRow>



            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1">

               <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_layout_ProductType"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:layout_weight="1">


                    <com.efftronics.android.eEmployee.Common.UI.CustomAutoCompleteTextView
                        android:id="@+id/actvProductType"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:hint="@string/product_type"
                        android:inputType="textCapWords|textMultiLine"
                        android:maxLength="250"
                        app:hintTextAppearance="@style/AutocompleteTheme" />
                </android.support.design.widget.TextInputLayout>

            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1">

                <LinearLayout
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:layout_weight="1" android:baselineAligned="false">

                    <android.support.design.widget.TextInputLayout
                        android:id="@+id/input_layout_FromDate"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:layout_weight="0.4">

                        <EditText
                            android:id="@+id/edtFromDate"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.4"
                            android:inputType="date"
                            android:hint="@string/from_date"
                            android:textColor="@android:color/black"
                            android:theme="@style/AutocompleteTheme" />
                    </android.support.design.widget.TextInputLayout>

                    <android.support.design.widget.TextInputLayout
                        android:id="@+id/input_layout_FromTime"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:layout_weight="0.6">

                        <EditText
                            android:id="@+id/edtFromTime"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.6"
                            android:inputType="none"
                            android:hint="@string/from_time"
                            android:textColor="@android:color/black"
                            android:theme="@style/AutocompleteTheme" />
                    </android.support.design.widget.TextInputLayout>
                </LinearLayout>
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1">
                <LinearLayout
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:layout_weight="1" android:baselineAligned="false">

                    <android.support.design.widget.TextInputLayout
                        android:id="@+id/input_layout_toDate"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:layout_weight="0.4">

                        <EditText
                            android:id="@+id/edtToDate"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.4"
                            android:inputType="date"
                            android:hint="@string/to_date"
                            android:textColor="@android:color/black"
                            android:theme="@style/AutocompleteTheme" />
                    </android.support.design.widget.TextInputLayout>

                    <android.support.design.widget.TextInputLayout
                        android:id="@+id/input_layout_toTime"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:layout_weight="0.6">

                        <EditText
                            android:id="@+id/edtToTime"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.6"
                            android:inputType="none"
                            android:hint="@string/to_time"
                            android:textColor="@android:color/black"
                            android:theme="@style/AutocompleteTheme" />
                    </android.support.design.widget.TextInputLayout>
                </LinearLayout>

            </TableRow>

actventryactventryDivNameactvProductType在xml文件中有三个CustomAutoCompleteTextViews

该方法适用于actventry但不适用于actventryDivNameactvProductType

actvDivNameactvEntryCategoryCustomAutoCompleteTextViews。同样,我也写了这个方法来保留一个CustomAutoCompleteTextView

我的布局中有三个CustomAutoCompleteTextView,但此方法仅针对第一个CustomAutoCompleteTextView调用,而不是针对其余两个调用。

我是第二次发布这个问题,因为没人响应,我没有得到解决方案。

我的代码有什么问题?我是android的新手。帮帮我

Errors are coming

0 个答案:

没有答案