清除Edittext后,焦点会更改为listview

时间:2016-01-28 10:02:22

标签: java android listview

我使用广播接收器来显示对话框。所以代码流如下:

Step1 获取requestCode值

Step2 根据此requestCode,broadCast接收器转到if或else if或else part

Step3 如果我使用某些扫描仪进入EditText(即扫描)所输入的值不匹配则显示Toast" Item Not Available"。

第4步一旦"项目不可用" toast带来了对Listview的焦点变化,这是我的问题。

Step5 再次,如果我将值传递给Scan EditText,Listview会自动点击。

所以我的问题是"如何从Listview中移除焦点"并将其设置为EditText(即扫描)。

供参考我将快照附加到代码段和layout.xml。请查看并删除您为何将重点放在列表视图上的建议。

enter image description here

.java片段

 final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
loc = mspinner.getItemAtPosition(mspinner.getSelectedItemPosition())
                        .toString();

                final String ItemNo;
                final String Desc;
                final String StockUnit;

                final String PickSeq;

                final String qtyCount;
                final String qtyonHand;
                final Button mok;
                final Button mcancel;
                final Button mplus;
                final Button mminus;
                final EditText medtQtyCount;
                final EditText medtItem;
                final EditText medtdesc;
                final EditText medtuom;
                final DatabaseHandler dbHandler;
                final String[] UOM = null;
                int requestCode;

                LayoutInflater li = LayoutInflater.from(InventoryCount.this);
                View promptsView = li.inflate(R.layout.quantityupdate, null);

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        InventoryCount.this);
                alertDialogBuilder.setView(promptsView);

                //requestCode=Integer.parseInt(intent.getStringExtra("idx"));
                requestCode=intent.getIntExtra("idx", -1);
                // create alert dialog
                final AlertDialog alertDialog = alertDialogBuilder.create();

                dbHandler = new DatabaseHandler(InventoryCount.this);
                medtuom = (EditText) promptsView.findViewById(R.id.edt_mseshipuom_mic);
                mok = (Button) promptsView.findViewById(R.id.btn_mseshipOk_mic);
                mcancel = (Button) promptsView.findViewById(R.id.btn_mseshipCancel_mic);
                mplus = (Button) promptsView.findViewById(R.id.btn_mseshipIncr_mic);
                mminus = (Button) promptsView.findViewById(R.id.btn_mseshipDecr_mic);
                medtQtyCount = (EditText) promptsView
                        .findViewById(R.id.edt_shipShiped_mic);
                medtdesc = (EditText) promptsView
                        .findViewById(R.id.edt_mseshipQtyOrd_mic);
                medtItem = (EditText) promptsView
                        .findViewById(R.id.edt_mseshipItemNo_mic);

    if (requestCode == 1) {
    }
    else if (requestCode == 0) {
                    // ItemNo
                    /*if (resultCode == RESULT_OK) {
                        Log.i("Scan resul format: ",
                                intent.getStringExtra("SCAN_RESULT_FORMAT"));
*/
                        String itNo = intent.getStringExtra("SCAN_RESULT");

                        dbhelper.getReadableDatabase();
                        MIC_Inventory mic_inventory = dbhelper.getMicInventoryDetails(
                                loc, itNo);
                        dbhelper.closeDatabase();

                        if (mic_inventory != null) {

                            loc = mspinner.getItemAtPosition(
                                    mspinner.getSelectedItemPosition()).toString();

                            ItemNo = mic_inventory.getItemno();
                            Desc = mic_inventory.getItemdescription();
                            PickSeq = mic_inventory.getPickingseq();
                            StockUnit = mic_inventory.getStockunit();
                            qtyonHand = mic_inventory.getQoh();// This value gives
                                                                // QOHand
                            qtyCount = mic_inventory.getQc();

                            medtItem.setText(ItemNo);
                            medtdesc.setText(Desc);
                            medtQtyCount.setText(qtyCount);
                            medtuom.setText(StockUnit);

                            mplus.setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View arg0) {
                                    // TODO Auto-generated method stub
                                    String a = medtQtyCount.getText().toString();
                                    int b = Integer.parseInt(a);
                                    b = b + 1;
                                    a = a.valueOf(b);
                                    medtQtyCount.setText(a);
                                }
                            });
                            mminus.setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                    int c = Integer.parseInt(medtQtyCount.getText()
                                            .toString());
                                    c = c - 1;
                                    medtQtyCount.setText(new Integer(c).toString());
                                }
                            });
                            mok.setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                    /*
                                     * UOM[mspinnerUom.getSelectedItemPosition()] =
                                     * medtQtyCount .getText().toString();
                                     */
                                    MIC_UOMInternal mic_uom = new MIC_UOMInternal();
                                    mic_uom.setLocation(loc);
                                    mic_uom.setItemno(ItemNo);
                                    String updatedqtyCount = medtQtyCount.getText()
                                            .toString();

                                    if (!qtyCount.equals(updatedqtyCount)) {
                                        mic_uom.setQc(Double
                                                .parseDouble(updatedqtyCount));
                                        mic_uom.setUom(StockUnit);

                                        MIC_Inventory mic_Inventory = new MIC_Inventory();
                                        mic_Inventory.setItemdescription(Desc);
                                        mic_Inventory.setItemno(ItemNo);
                                        mic_Inventory.setLocation(loc);
                                        mic_Inventory.setPickingseq(PickSeq);
                                        mic_Inventory.setQc(updatedqtyCount);
                                        mic_Inventory.setQoh(qtyonHand);
                                        mic_Inventory.setStockunit(StockUnit);

                                        dbHandler.getWritableDatabase();
                                        String result = dbHandler
                                                .insertIntoInternal(mic_uom);
                                        if (result.equals("success")) {
                                            result = dbHandler.updateMIC(mic_Inventory);
                                        }
                                        dbHandler.closeDatabase();
                                    }
                                    Intent i = new Intent(InventoryCount.this,
                                            InventoryCount.class);
                                    i.putExtra("et", 1);
                                    i.putExtra("LOCATION", loc);
                                    // i.putExtra("ID", ID);
                                    startActivity(i);
                                    // InventoryCount.this.finish();

                                }
                            });
                            mcancel.setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                    alertDialog.cancel();

                                }
                            });

                            // show it
                            alertDialog.show();

                        } else {
                            /*
                             * Toast.makeText(this, "Item not available",
                             * Toast.LENGTH_LONG).show();
                             */


                            toastText.setText("Item not available");
                            Toast toast = new Toast(getBaseContext());
                            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 410);
                            toast.setDuration(Toast.LENGTH_SHORT);
                            toast.setView(toastLayout);
                            toast.show();


                            msearchtext.setText("");
                            /*msearchtext.setFocusableInTouchMode(true);
                            msearchtext.requestFocus();*/
                            /*msearchtext.setSelection(0);
                            lstView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
                            */msearchtext.requestFocus();

                        }
    else if (requestCode == 2) {
    }
    else
                {
                    toastText.setText("Problem in Scanning");
                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 410);
                    toast.setDuration(Toast.LENGTH_SHORT);
                    toast.setView(toastLayout);
                    toast.show();
                }
}

Layout.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/border_green"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/txt_InvTitle"
        style="@style/pageTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:text="@string/invTitle" />

    <View
        android:id="@+id/txt_InvView"
        android:layout_width="match_parent"
        android:layout_height="2dip"
        android:layout_below="@+id/txt_InvTitle"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#2E9AFE" />

    <LinearLayout
        android:id="@+id/invLocation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/txt_InvView"
         android:layout_marginTop="16dp" >

        <TextView
            android:id="@+id/txtLoc"
            style="@style/textRegular"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="left|center"
            android:text="@string/location" />

        <Spinner
            android:id="@+id/sploc"
            style="@style/SpinnerItemAppTheme"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:editable="false" />
    </LinearLayout>


    <LinearLayout
        android:id="@+id/invScanType"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/invLocation"
        android:layout_gravity="center"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="3dp"
        android:layout_marginTop="18dp"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/edt_Search_mic"
            style="@style/EditTextAppTheme_Scan"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dp"
            android:layout_weight=".15"
            android:gravity="center"            
            android:hint="@string/scan" />


        <RadioGroup
            android:id="@+id/radioScanBasedOn_mic"
            style="@style/RadioButtonAppTheme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radioInum_mic"
                style="@style/textRegular"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight=".25"
                 android:button="@drawable/radiobutton_selector"
                android:checked="true"
                  android:drawablePadding="50dp"
                android:paddingLeft="10dip"
                android:text="@string/itemno" />

            <RadioButton
                android:id="@+id/radioNum_mic"
                style="@style/textRegular"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:button="@drawable/radiobutton_selector"
                android:checked="false"
                android:layout_marginRight="5dp"
                android:layout_weight=".25"
                  android:drawablePadding="50dp"
                android:paddingLeft="10dip"
                android:text="@string/manfno" />

            <RadioButton
                android:id="@+id/radioUpc_mic"
                style="@style/textRegular"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:button="@drawable/radiobutton_selector"
                android:checked="false"
                android:layout_marginRight="5dp"
                android:layout_weight=".25"
                  android:drawablePadding="50dp"
                android:paddingLeft="10dip"
                android:text="@string/upc" />
        </RadioGroup>
    </LinearLayout>

    <HorizontalScrollView
        android:id="@+id/scroll_full_mic"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/invScanType" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginTop="25dp"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/lay_fullTitle_mic"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#000000"
                android:orientation="horizontal"
                android:padding="5dp" >

                <TextView
                    style="@style/textRegular_list"
                    android:layout_width="105dp"
                    android:layout_height="wrap_content"
                    android:text="@string/itemno"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textStyle="bold" />

                <TextView
                    style="@style/textRegular_list"
                    android:layout_width="130dp"
                    android:layout_height="wrap_content"
                    android:gravity="center|left"
                    android:text="@string/description"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textStyle="bold" />

                <TextView
                    style="@style/textRegular_list"
                    android:layout_width="140dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="4dp"
                    android:gravity="center|left"
                    android:text="@string/pick_seq"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textStyle="bold" />

                <TextView
                    style="@style/textRegular_list"
                    android:layout_width="120dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="4dp"
                    android:gravity="center|left"
                    android:text="@string/qoh"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textStyle="bold" />

                <TextView
                    style="@style/textRegular_list"
                    android:layout_width="120dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="4dp"
                    android:gravity="center|left"
                    android:text="@string/qc"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textStyle="bold" />

                <TextView
                    style="@style/textRegular_list"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="4dp"
                    android:gravity="center|left"
                    android:text="@string/uom"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textStyle="bold" />
            </LinearLayout>

            <ListView
                android:id="@+id/lst_msefull_mic"
                android:layout_width="match_parent"
                android:layout_height="match_parent"

                style="@style/ListViewAppTheme.White" >
            </ListView>
        </LinearLayout>
    </HorizontalScrollView>

    <LinearLayout
        android:id="@+id/lay_PO_mic"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="41dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:visibility="gone" >

        <Button
            android:id="@+id/btn_OrderLstImport_mic"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:textSize="18dp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/btn_OrderLstExport_mic"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:textSize="18dp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/btn_OrderLstExit_mic"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:textSize="18dp"
            android:textStyle="bold" />
    </LinearLayout>

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

添加一个textwatcher来编辑文本并检查文本何时不是空白且不等于预期文本然后只切换焦点。

/ *设置Text Watcher监听器* /

yourEditText.addTextChangedListener(passwordWatcher);

并在用户输入文字后检查文字

private final TextWatcher passwordWatcher = new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    public void afterTextChanged(Editable s) {
        if (s.length() != 0 && passwordEditText.getText().equals("Your expected text")) {
            // show your toast and change focus
        } 
    }
}

答案 1 :(得分:0)

您应该在不需要时使用setFocusable(false)使您的列表视图无法集中,当您从条形码扫描仪中正确获得响应时,您可以再次使列表视图具有可调整性。