java - Android 中的重置/清除按钮

标签 java android reset-button

我需要一个重置按钮,可以在单击时将所有 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 框出来并且一切都处于初始状态。

最佳答案

要从 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);
}
}

EDIT

在您的情况下,您应该在 xml 中使用提示,例如

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

然后你只能使用 setText("") 为每个 TextView 进入你的 clear() 方法,并删除所有 setOnFocusChangeListener,因为它将不再被使用。

关于java - Android 中的重置/清除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31699511/

相关文章:

java - 如何为neatebeans maven javafx项目安装好maven

java - Recyclerview 没有正确显示 SQlite 数据库数据

android - 标记 Android 版本

java - 如何在不显示的情况下使用 Universal ImageLoader 加载图像

java - 为什么在 java 中 android 第二次调用 web 服务时会创建新的 session

html - 使用图像作为重置按钮

java - Mule 默认捕获异常并停止流程

java - j2me和ksoap如何获取asp.net webservice中的特定元素