我需要一个重置按钮,在单击时重置所有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");
}
我不是很高兴这个重置,因为我有启动0.0在editText框无论如何。我想在这里实现重置:
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);
}
}
编辑
在你的情况下,你应该使用提示在xml
,如
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="0.0"/>
那么你只能在你的clear()
方法中对每个TextView
使用setText("")
,并删除所有的setOnFocusChangeListener
,因为它将不再使用
修改
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");
}
它可能对你有帮助。