简体   繁体   English

Android EditText .setText(“”)无法正常工作

[英]Android EditText .setText(“”) not working

I am trying to clear the text in my edit text field but for some reason it is not working. 我正在尝试清除我的编辑文本字段中的文本,但由于某种原因,它无法正常工作。

I have declared an edit text field in the xml of my view: 我在视图的xml中声明了一个编辑文本字段:

 <EditText
        android:id="@+id/enterGlucoseLevel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mySimpleXYPlot"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:inputType="numberDecimal" />

I have used the attribute onClick to link my button to the enterGlucoseAction() method in my MainActivity class: 我已经使用属性onClick将我的按钮链接到MainActivity类中的enterGlucoseAction()方法:

<Button
    android:id="@+id/enterGlucoseButton"
    android:clickable="true"
    android:onClick= "enterGlucoseAction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/enterGlucoseLevel"
    android:layout_centerHorizontal="true"
    android:text="@string/enterGlucoseLvlButton" />

Here is that method: 这是该方法:

public void enterGlucoseAction(View v) {
    glucoseField.setText("");

     Toast.makeText(v.getContext(), "You clicked the button", Toast.LENGTH_SHORT).show();
}

I get the toast pop up but my edit text field is not being cleared. 我弹出烤面包,但是我的编辑文本字段没有被清除。

Here is my on create and instance variables in case the problem lies there. 如果问题出在这里,这是我的create和instance变量。

private XYPlot plot;
private Button addGlucoseLevel;
private EditText glucoseField;
private XYSeries currentSeries;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    addGlucoseLevel = (Button) findViewById(R.id.enterGlucoseButton);
    glucoseField = (EditText) findViewById(R.id.enterGlucoseLevel);                     

    currentSeries = new SimpleXYSeries(Arrays.asList(new Number[] { 7200, 8, 54000, 2, 64800, 4 }), 
            SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED,
            "Glucose Level");

    //Create an series of numbers to plot.
    createGraph(currentSeries);
}

Thanks in advance. 提前致谢。

And sorry, I know this has been asked before but I can't find a solution to my problem specifically. 抱歉,我知道之前已经有人问过这个问题,但是我找不到专门解决我问题的方法。

To clear your EditText which accepts numbers use clear() method: 要清除接受数字的EditText ,请使用clear()方法:

glucoseField.getText().clear();

You can try setting it this way: 您可以尝试通过以下方式进行设置:

((EditText)findViewById(R.id.enterGlucoseLevel)).getText().clear();

Alternatively you can use getText().clear(); 另外,您可以使用getText().clear(); just replace glucoseField.setText(""); 只需替换glucoseField.setText(""); with glucoseField.getText().clear(); glucoseField.getText().clear();

You should use 你应该用

setText("\b");

As we can use escape sequence 因为我们可以使用转义序列

((EditText)findViewById(R.id.yourEditTextId)).getText().clear();

其他都崩溃了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM