如何在EditText中自动显示结果?

时间:2017-07-10 16:36:45

标签: java android currency

我正在开发一个将值从kW转换为km的应用程序,当我在EditText1中输入值而不使用任何按钮在视图中自动执行此操作时,我希望看到EditText2中的分数,但我没有想法如何做到这一点。现在我必须从Spinner中选择正确的值才能在EditText2中获得结果。这怎么改变?也许是以错误的方式我正在寻找Spinner的搜索值?我还会问你如何改进应用程序。谢谢。

我尝试使用TextWeatcher,但它仍无效。

MainActivity.java

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {


    Spinner spinner1, spinner2;
    EditText editText1, editText2;
    double count = 1.36;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner2 = (Spinner) findViewById(R.id.spinner2);

        editText1 = (EditText) findViewById(R.id.editText1);
        editText2 = (EditText) findViewById(R.id.editText2);

        editText1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after){
            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {

                String c = s.toString();
                editText2.setText(c);
            }
        });

        ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.przelicznik_arrayKM,
                android.R.layout.simple_spinner_item);

        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner1.setAdapter(adapter1);
        spinner1.setOnItemSelectedListener(this);


        ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.przelicznik_arraykW,
                android.R.layout.simple_spinner_item);

        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner2.setAdapter(adapter2);

        spinner2.setOnItemSelectedListener(this);

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        Spinner spinner = (Spinner) parent;
        String text = spinner1.getSelectedItem().toString();

        String text2 = spinner2.getSelectedItem().toString();


        if(text.equals("kW") & text2.equals("KM") ) {

            try {
                double a = Double.parseDouble(editText1.getText().toString());
                double suma = a / count;

                editText2.setText(String.valueOf(suma));

            }catch (NumberFormatException e){
                Toast.makeText(this, "Enter yout value!", Toast.LENGTH_SHORT).show();
            }

        }

        if(text.equals("KM") & text2.equals("kW")){

             try {
                 double a = Double.parseDouble(editText1.getText().toString());
                 double suma = a /count;

                 editText2.setText(String.valueOf(suma));

             }catch (NumberFormatException e){
                 Toast.makeText(this, "Enter your value!", Toast.LENGTH_SHORT).show();
             }
         } else {
            String textString = editText1.getText().toString();
            editText2.setText(String.valueOf(textString));
          }
        }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {}

    public void clearEditText(View view) {

        editText1.setText("");
        editText2.setText("");
    }
}

Strings.xml

    <resources>
    <string name="app_name">PrzelicznikMocySilnika</string>

    <string-array name="przelicznik_arraykW">
        <item>kW</item>
        <item>KM</item>
    </string-array>

    <string-array name="przelicznik_arrayKM">
        <item>KM</item>
        <item>kW</item>
    </string-array>



</resources>

Layout.xmln

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.hfad.przelicznikmocysilnika.MainActivity">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        android:hint="Enter your value"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_toLeftOf="@+id/spinner1"
        android:layout_toStartOf="@+id/spinner1" />

    <EditText
        android:id="@+id/editText2"
        android:hint="Enter your value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/editText1"
        android:ems="10"
        android:inputType="number"
        android:layout_toLeftOf="@+id/spinner2"
        android:layout_toStartOf="@+id/spinner2" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_above="@+id/editText2" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/editText1" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:text="Reset"
        android:textAllCaps="false"
        android:onClick="clearEditText"/>

</RelativeLayout>

4 个答案:

答案 0 :(得分:1)

使用onTextChanged方法

执行操作

要获取当前值,请使用editText1.getText().toString()

答案 1 :(得分:0)

在onTextChanged而不是afterTextChanged

中添加你的逻辑
editText1.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after){
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String c = editText1.getText().toString();
            editText2.setText(c);
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
  }

答案 2 :(得分:0)

对于两个EditText视图之间的双向转换,只有在edittext处于焦点时才需要进行转换。示例代码如下。

editText1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editText1.isFocused()) {
                    String str = editText1.getText().toString().trim();
                    if (!str.isEmpty()) {
                        //your conversion logic here
                        editText2.setText(String.valueOf(convertedValue));
                    }else{
                        etTo.setText("0");
                    }
                }
            }
        });

尝试使用类似的方法从editText2转换为editText1。

答案 3 :(得分:-1)

使用侦听器进行文本更改。

your_edittext.addTextChangedListener(new InputValidator());

private class InputValidator implements TextWatcher {

    public void afterTextChanged(Editable s) {

    }    
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {                

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

    }    
}    

}