2

クリック時にすべての editText ボックスを初期状態にリセットするリセット ボタンが必要です。現時点で私はこれを持っています:

    main_xml
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ClearButton"
    android:id="@+id/ClearButton"
    android:onClick="clear"/>

java

     public void clear(View v) {
        TextBox1.setText("0.0");
        TextBox2.setText("0.0");
    } 

とにかく、editTextボックスで起動時に0.0を持っているので、このリセットにはあまり満足していません。ここのどこかにリセットを実装したいと思います:

   input3 = String.valueOf(TextBox1.getText());
    flowRate.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if ((hasFocus)&&(input3.equals("0.0") || input3.equals(""))) {
                TextBox1.setText("");
            }
            input3 = (String.valueOf(TextBox1.getText()));
            if ((!hasFocus) && (input3.equals(""))){
                TextBox1.setText("0.0");
            }
        }
    });
    // remove on touch the value entries in the Flow rate field to start calculation
    // if no value is entered the field will return to his normal state (0.0)
    input2 = String.valueOf(TextBox2.getText());
    TextBox2.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if ((hasFocus)&&(input2.equals("0.0") || input2.equals(""))) {
                TextBox2.setText("");
            }
            input2 = (String.valueOf(TextBox2.getText()));
            if ((!hasFocus) && (input2.equals(""))){
                TextBox2.setText("0.0");
            }
        }
    });

リセットして、フォーカスが editText ボックスにある瞬間に、ボックスに値を書き込む前に、最初に 0.0 を削除する必要があります。editTextボックスにフォーカスが出て、すべてが初期状態になるようにリセットしたいと思います。

4

2 に答える 2

0

からピントを合わせるにはeditbox、 を使用するだけclearFocus()です。

TextBox1.clearFocus();
TextBox2.clearFocus();

これが私のコードです

public class MainActivity extends AppCompatActivity {
String input1, input2, input3;
EditText tankVolume, fillingPressure, flowRate;
TextView fillingTime;
Button calculate, ClearButton;


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

    tankVolume=(EditText)findViewById(R.id.tankVolume);
    fillingPressure=(EditText)findViewById(R.id.fillingPressure);
    flowRate=(EditText)findViewById(R.id.flowRate);
    fillingTime=(TextView)findViewById(R.id.fillingTime);
    calculate=(Button)findViewById(R.id.calculate);

    // remove on touch the value entries in the Flow rate field to start calculation
    // if no value is entered the field will return to his normal state (0.0)
    input3 = String.valueOf(flowRate.getText());
    flowRate.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if ((hasFocus)&&(input3.equals("0.0") || input3.equals(""))) {
                flowRate.setText("");
            }
            input3 = (String.valueOf(flowRate.getText()));
            if ((!hasFocus) && (input3.equals(""))){
                flowRate.setText("0.0");
            }
        }
    });
    // remove on touch the value entries in the Flow rate field to start calculation
    // if no value is entered the field will return to his normal state (0.0)
    input2 = String.valueOf(fillingPressure.getText());
    fillingPressure.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if ((hasFocus)&&(input2.equals("0.0") || input2.equals(""))) {
                fillingPressure.setText("");
            }
            input2 = (String.valueOf(fillingPressure.getText()));
            if ((!hasFocus) && (input2.equals(""))){
                fillingPressure.setText("0.0");
            }
        }
    });
     // remove on touch the value entries in the Flow rate field to start calculation
    // if no value is entered the field will return to his normal state (0.0)
    input1 = String.valueOf(tankVolume.getText());
    tankVolume.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if ((hasFocus)&&(input1.equals("0.0") || input1.equals(""))) {
                tankVolume.setText("");
            }
            input1 = (String.valueOf(tankVolume.getText()));
            if ((!hasFocus) && (input1.equals(""))){
                tankVolume.setText("0.0");
            }

        }
    });

    // calculate the filling time
    calculate.setOnClickListener(new Button.OnClickListener()

    {
        public void onClick
                (View v) {
            calculate();
        }
    });
}

private void calculate()
{

    Double value1 = Double.parseDouble(tankVolume.getText().toString());
    Double value2 = Double.parseDouble(fillingPressure.getText().toString());
    Double value3 = Double.parseDouble(flowRate.getText().toString());
    Double calculatedValue = (value1*value2)/value3;


    fillingTime.setText(calculatedValue.toString());
}



    // reset everything to zero
    public void clear(View v) {
        tankVolume.setText("0.0");
        fillingPressure.setText("0.0");
        flowRate.setText("0.0");
        fillingTime.setText("0");
        tankVolume.clearFocus();
        fillingPressure.clearFocus();
        flowRate.clearFocus();
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

編集

あなたの場合、次のxmlようなヒントを使用する必要があります

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="0.0"/>

setText("")次に、 for eachのみをメソッドに使用TextViewclear()、 all を削除しますsetOnFocusChangeListener。これは使用されなくなるためです。

于 2015-07-29T12:32:34.003 に答える
-1

から変更する

public void clear(View v) {
        TextBox1.setText("0.0");
        TextBox2.setText("0.0");
    } 

public void clear(View v) {
        TextBox1.setHint("0.0");
        TextBox2.setHint("0.0");
    } 

それはあなたを助けるかもしれません。

于 2015-07-29T15:25:37.797 に答える