Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android onFocusChangeListener不工作_Android - Fatal编程技术网

Android onFocusChangeListener不工作

Android onFocusChangeListener不工作,android,Android,我为android计算器应用程序编写了一些代码。我想要的是:- 如果EditText字段失去焦点,所有输出字段都应该像在excel工作表中一样得到更新,如果我们更新单元格中的任何值,它会更新工作表中所有连接的值。 但是我的代码没有做任何事情。如果我不使用trycatch块,则应用程序正在崩溃。它显示代码中有一个错误,但我无法跟踪它 请帮助,如果有更好的方法来做这个货币面额合计计算器 Java代码是:- package mahalaxmi.mahalaxmi; import android.s

我为android计算器应用程序编写了一些代码。我想要的是:-

如果
EditText
字段失去焦点,所有输出字段都应该像在excel工作表中一样得到更新,如果我们更新单元格中的任何值,它会更新工作表中所有连接的值。

但是我的代码没有做任何事情。如果我不使用
try
catch
块,则应用程序正在崩溃。它显示代码中有一个错误,但我无法跟踪它

请帮助,如果有更好的方法来做这个货币面额合计计算器

Java代码是:-

package mahalaxmi.mahalaxmi;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by SIV on 10-03-2018.
 */

public class Fragment_four extends Fragment {


    private static EditText notes2000, notes500, notes200, notes100, notes50, notes20, notes10, notes5, notes2, notes1;
    private static TextView value2000, value500, value200, value100, value50, value20, value10, value5, value2, value1;
    private static TextView errorText41, quantityTotal, valueTotal;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout, container, false);


        notes2000 = (EditText) view.findViewById(R.id.qty2000);
        notes500 = (EditText) view.findViewById(R.id.qty500);
        notes200 = (EditText) view.findViewById(R.id.qty200);
        notes100 = (EditText) view.findViewById(R.id.qty100);
        notes50 = (EditText) view.findViewById(R.id.qty50);
        notes20 = (EditText) view.findViewById(R.id.qty20);
        notes10 = (EditText) view.findViewById(R.id.qty10);
        notes5 = (EditText) view.findViewById(R.id.qty5);
        notes2 = (EditText) view.findViewById(R.id.qty2);
        notes1 = (EditText) view.findViewById(R.id.qty1);


        value2000 = (TextView) view.findViewById(R.id.result2000);
        value500 = (TextView) view.findViewById(R.id.result500);
        value200 = (TextView) view.findViewById(R.id.result200);
        value100 = (TextView) view.findViewById(R.id.result100);
        value50 = (TextView) view.findViewById(R.id.result50);
        value20 = (TextView) view.findViewById(R.id.result20);
        value10 = (TextView) view.findViewById(R.id.result10);
        value5 = (TextView) view.findViewById(R.id.result5);
        value2 = (TextView) view.findViewById(R.id.result2);
        value1 = (TextView) view.findViewById(R.id.result1);
        quantityTotal = (TextView) view.findViewById(R.id.qtyTotal);
        valueTotal = (TextView) view.findViewById(R.id.resultTotal);
        errorText41 = (TextView) view.findViewById(R.id.errorText4);




        View.OnFocusChangeListener listener;

        listener = new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                short val2000=0, val500=0, val200=0, val100=0, val50=0, val20=0, val10=0, val5=0, val2=0, val1=0;
                int totalQty, totalValue;
                int tval2000=0, tval500=0, tval200=0, tval100=0, tval50=0, tval20=0, tval10=0, tval5=0, tval2=0, tval1=0;
                if(!hasFocus){
                    try {

                        //extracting the entered values from EditText field
                        val2000 = Short.parseShort(value2000.getText().toString());
                        val500 = Short.parseShort(value500.getText().toString());
                        val200 = Short.parseShort(value200.getText().toString());
                        val100 = Short.parseShort(value100.getText().toString());
                        val50 = Short.parseShort(value50.getText().toString());
                        val20 = Short.parseShort(value20.getText().toString());
                        val10 = Short.parseShort(value10.getText().toString());
                        val5 = Short.parseShort(value5.getText().toString());
                        val2 = Short.parseShort(value2.getText().toString());
                        val1 = Short.parseShort(value1.getText().toString());

                        //calculating the output values
                        tval2000 = value(notes2000, val2000);
                        tval500 = value(notes500, val500);
                        tval200 = value(notes200, val200);
                        tval100 = value(notes100, val100);
                        tval50 = value(notes50, val50);
                        tval20 = value(notes20, val20);
                        tval10 = value(notes10, val10);
                        tval5 = value(notes5, val5);
                        tval2 = value(notes2, val2);
                        tval1 = value(notes1, val1);

                        //setting the output value

                        value2000.setText(tval2000 + "");
                        value500.setText(tval500 + "");
                        value200.setText(tval200 + "");
                        value100.setText(tval100 + "");
                        value50.setText(tval50 + "");
                        value20.setText(tval20 + "");
                        value10.setText(tval10 + "");
                        value5.setText(tval5 + "");
                        value2.setText(tval2 + "");
                        value1.setText(tval1 + "");

                        totalQty = val2000 + val500 + val200 + val100 + val50 + val20 + val10 + val5 + val2 + val1;
                        totalValue = tval2000 + tval500 + tval200 + tval100 + tval50 + tval20 + tval10 + tval5 + tval2 + tval1;

                        quantityTotal.setText(totalQty + "");
                        valueTotal.setText(totalValue + "");

                    }

                    catch (Exception e){};



                }
      }
        };



        notes2000.setOnFocusChangeListener(listener);
        notes500.setOnFocusChangeListener(listener);
        notes200.setOnFocusChangeListener(listener);
        notes100.setOnFocusChangeListener(listener);
        notes50.setOnFocusChangeListener(listener);
        notes20.setOnFocusChangeListener(listener);
        notes10.setOnFocusChangeListener(listener);
        notes5.setOnFocusChangeListener(listener);
        notes2.setOnFocusChangeListener(listener);
        notes1.setOnFocusChangeListener(listener);



        return view;

    }

    private void clearQty(EditText e) {
        e.setText("");
    }

    private int value(int e, int s) {

        return e * s;
    }


}
布局文件为:-

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteY="81dp">

    <TableLayout
        android:layout_width="351dp"
        android:layout_height="506dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="50dp">

            <TextView
                android:id="@+id/lableDenom"
                android:layout_width="wrap_content"
                android:layout_height="35dp"
                android:background="@color/colorPrimaryDark"
                android:text="Denom"
                android:textColor="@android:color/background_light"
                android:textSize="25dp"
                android:textStyle="bold"
                tools:layout_editor_absoluteX="16dp"
                tools:layout_editor_absoluteY="14dp" />

            <TextView
                android:id="@+id/lableQty"
                android:layout_width="wrap_content"
                android:layout_height="35dp"
                android:background="@color/colorPrimaryDark"
                android:paddingLeft="10dp"
                android:text="Qty"
                android:textAllCaps="false"
                android:textColor="@android:color/background_light"
                android:textSize="25dp"
                android:textStyle="bold|italic"
                tools:layout_editor_absoluteX="16dp"
                tools:layout_editor_absoluteY="14dp" />


            <TextView
                android:id="@+id/lableValue"
                android:layout_width="184dp"
                android:layout_height="35dp"
                android:background="@color/colorPrimaryDark"
                android:text="Value"
                android:textColor="@android:color/background_light"
                android:textSize="25dp"
                android:textStyle="bold"
                tools:layout_editor_absoluteX="16dp"
                tools:layout_editor_absoluteY="14dp" />


        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="50dp">

            <TextView
                android:id="@+id/lable2000"
                android:layout_width="67dp"
                android:layout_height="35dp"
                android:background="@color/Rs_2000"
                android:paddingLeft="5dp"
                android:text="2000"
                android:textColor="@android:color/background_light"
                android:textSize="25dp"
                android:textStyle="bold"
                tools:layout_editor_absoluteX="16dp"
                tools:layout_editor_absoluteY="14dp" />


            <EditText
                android:id="@+id/qty2000"
                android:layout_width="100dp"
                android:layout_height="40dp"
                android:ems="10"
                android:inputType="number"
                android:maxLength="4"
                android:textColor="@android:color/holo_blue_dark"
                android:textSize="25dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/result2000"
                android:layout_width="184dp"
                android:layout_height="35dp"
                android:background="@color/Rs_2000"
                android:textColor="@android:color/background_light"
                android:textSize="25dp"
                android:textStyle="bold"
                tools:layout_editor_absoluteX="16dp"
                tools:layout_editor_absoluteY="14dp" />


        </TableRow>

        ....
        and so on
        ....

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="50dp">

            <TextView
                android:id="@+id/lableTotal"
                android:layout_width="67dp"
                android:layout_height="35dp"
                android:layout_marginTop="5dp"
                android:background="@color/colorPrimaryDark"
                android:paddingLeft="5dp"
                android:text="Total"
                android:textColor="@android:color/background_light"
                android:textSize="25dp"
                android:textStyle="bold"
                tools:layout_editor_absoluteX="16dp"
                tools:layout_editor_absoluteY="14dp" />


            <TextView
                android:id="@+id/qtyTotal"
                android:layout_width="100dp"
                android:layout_height="35dp"
                android:layout_marginTop="5dp"
                android:background="@color/colorPrimaryDark"
                android:ems="10"
                android:inputType="number"
                android:maxLength="4"
                android:textColor="@android:color/background_light"
                android:textSize="25dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/resultTotal"
                android:layout_width="184dp"
                android:layout_height="35dp"
                android:layout_marginTop="5dp"
                android:background="@color/colorPrimaryDark"
                android:textColor="@android:color/background_light"
                android:textSize="25dp"
                android:textStyle="bold"
                tools:layout_editor_absoluteX="16dp"
                tools:layout_editor_absoluteY="14dp" />


        </TableRow>

    </TableLayout>

    <TextView
        android:id="@+id/errorText4"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="42dp"
        tools:layout_editor_absoluteY="460dp" />

</android.support.constraint.ConstraintLayout>

....
等等
....

您已为所有编辑文本指定了相同的侦听器-View.onfocuschangestener()。每次单击EditText项时都会触发侦听器功能。因此,当第一项的焦点改变时,将调用listener,您将尝试读取所有EditText值并将其转换为Short

在这种情况下,所有其他EditText没有值,这将触发NumberFormatException

请参阅下面的实现,它将给出在多个视图上实现同一个侦听器的想法

int val2000=0, val500=0, val200=0, val100=0, val50=0, val20=0, val10=0, val5=0, val2=0, val1=0;
int totalQty, totalValue;
int tval2000=0, tval500=0, tval200=0, tval100=0, tval50=0, tval20=0, tval10=0, tval5=0, tval2=0,tval1=0;

View.OnFocusChangeListener listener;
    listener = new View.OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {

            if(!hasFocus){
                try {

                    switch(v.getId()) {
                        case R.id.qty2000:
                            val2000 = Integer.parseInt(notes2000.getText().toString());
                            tval2000 = value(2000, val2000);
                            value2000.setText(tval2000 + "");
                            break;

                        case R.id.qty500 :
                            val500 = Integer.parseInt(notes500.getText().toString());;
                            tval500 = value(500, val500);
                            value500.setText(tval500 + "");
                            break;

                        case R.id.qty200:
                            val200 = Integer.parseInt(notes200.getText().toString());
                            tval200 = value(200, val200);
                            value200.setText(tval200 + "");
                            break;


                        case R.id.qty100:
                            val100 = Integer.parseInt(notes100.getText().toString());
                            tval100 = value(100, val100);
                            value100.setText(tval100 + "");
                            break;

                        case R.id.qty50:
                            val50 = Integer.parseInt(notes50.getText().toString());
                            tval50 = value(50, val50);
                            value50.setText(tval50 + "");
                            break;

                        case R.id.qty20:
                            val20 = Integer.parseInt(notes20.getText().toString());
                            tval20 = value(20, val20);
                            value20.setText(tval20 + "");
                            break;

                        case R.id.qty10:
                            val2000 = Integer.parseInt(notes10.getText().toString());
                            tval10 = value(10, val10);
                            value10.setText(tval10 + "");
                            break;


                        case R.id.qty5:
                            val5 = Integer.parseInt(notes5.getText().toString());
                            tval5 = value(5, val5);
                            value5.setText(tval5 + "");
                            break;


                        case R.id.qty2:
                            val2 = Integer.parseInt(notes2.getText().toString());
                            tval2 = value(2, val2);
                            value2.setText(tval2 + "");
                            break;

                        case R.id.qty1 :
                            val1 = Integer.parseInt(notes1.getText().toString());
                            tval1 = value(1, val1);
                            value1.setText(tval1 + "");
                            break;

                        default:
                            //do the default behaviour.
                            break;
                    }
                }

                catch (Exception e){
                    e.printStackTrace();
                }

                finally {

                    totalQty = val2000 + val500 + val200 + val100 + val50 + val20 + val10 + val5 + val2 + val1;
                    totalValue = tval2000 + tval500 + tval200 + tval100 + tval50 + tval20 + tval10 + tval5 + tval2 + tval1;

                    quantityTotal.setText(totalQty + "");
                    valueTotal.setText(totalValue + "");
                }
            }
        }
    };

请为每个视图创建单独的侦听器,或为每个视图编写开关大小写 在听者内部

switch(view.getId){
    case: R.id.your_view_id:
    break;
}

使用e.printstacktrace()打印错误堆栈跟踪,而不是空的catch块写入,这样您就可以了解此错误。如果您可以更新包含此信息的帖子,我们也可以帮助您解决此问题。最初我开始单独编写,但我无法从其他EditText字段中选择值,如果任何EditText字段失去焦点,则更新所有总计字段和总计字段。更新了答案。请检查并让我知道这是否解决了问题。是的,它正在工作,但一个问题是total字段显示的EditText值正在失去焦点,相应的输出值,而不是所有字段的总和。请根据您的第一条评论检查更新的答案。我已经把所有的局部函数变量移到了类变量中。这将用屏幕上可见的值更新总值。听起来不错。祝你一切顺利!