Java Android中的重置/清除按钮
我需要一个重置按钮,可以在单击时将所有编辑文本框重置为初始状态。目前,我有以下几点:Java Android中的重置/清除按钮,java,android,reset-button,Java,Android,Reset Button,我需要一个重置按钮,可以在单击时将所有编辑文本框重置为初始状态。目前,我有以下几点: main_xml <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ClearButton" android:id="@+id/ClearButton" android:onClick="clea
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,然后才能在框中写入值。我希望在重置时,焦点从编辑文本框中出来,并且所有内容都处于初始状态。要从编辑框中出来焦点,只需使用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中的提示,例如
然后,您只能在clear方法中为每个TextView使用setText,并删除all setOnFocusChangeListener,因为它将不再使用。要从editbox中取出焦点,您只需使用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中的提示,例如
然后,您只能在clear方法中为每个TextView使用setText,并删除all setOnFocusChangeListener,因为它将不再使用。更改自
public void clear(View v) {
TextBox1.setText("0.0");
TextBox2.setText("0.0");
}
到
它可能会帮助您。从
public void clear(View v) {
TextBox1.setText("0.0");
TextBox2.setText("0.0");
}
到
它可能会对您有所帮助。为例如reset创建任何方法,现在在该方法中为EditText设置您想要的值,现在从任何地方调用它,但确保调用来自mainthread。为例如reset创建任何方法,现在在该方法中为EditText设置您想要的值,现在从任何地方调用它,但确保调用来自主线程。现在的问题是clearFocus我的键盘正在从数字键盘切换到qwerty键盘,而在焦点上它会切换回来。你到底在哪里尝试清除这两个EditText的焦点?我编辑你的文本并应用我的代码//将所有设置为零-位置是否可以在单击按钮时将整个活动重置为其原始状态?这可能不那么困难。看看你的代码,我想你需要使用提示,如下面的答案。我正在编辑我的答案,以便向您展示这样做的最佳方式。现在的问题是clearFocus我的键盘正在从数字键盘切换到qwerty键盘,并且在焦点上它会切换回来。您究竟在哪里尝试清除这两个EditText的焦点?我编辑您的文本并应用我的代码//将所有设置为零-位置是否可以在单击按钮时将整个活动重置为其原始状态?这可能不那么困难。看看你的代码,我想你需要使用提示,如下面的答案。我正在编辑我的答案,以便向您展示这样做的最佳方式。您通常在onCreate或xml中使用setHint,而不是在clearing方法中使用。如果您已经在xml文件中添加了提示,那么当您将空文本设置为edittext时,它将已经向您显示提示。是,因此,在这种情况下,上面的答案没有意义。当我重置并将焦点放在编辑文本框上时,我必须先删除0.0,然后才能在框中写入值。根据提示,您不必从edittext中清除焦点。。。。这就是为什么我建议你这么做。无论如何,这是你的决定你想要什么。哈哈,有一个误解,这不是我的问题。我刚刚发布了一个答案:您通常在onCreate或xml中使用setHint,而不是在clearing方法中使用。如果您已经在xml文件中添加了提示,那么当您将空文本设置为edittext时,它将显示提示。是,因此,在这种情况下,上面的答案没有意义。当我重置并将焦点放在编辑文本框上时,我必须先删除0.0,然后才能在框中写入值。根据提示,您不必从edittext中清除焦点。。。。这就是为什么我建议你这么做。无论如何,这是你的决定你想要什么。哈哈,有一个误解,这不是我的问题。我刚刚发布了一个答案哈哈
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");
}