Android Form Reset?

后端 未结 3 1390
半阙折子戏
半阙折子戏 2021-01-26 22:16

I have a simple calculator that has six EditText views where users will enter numbers to perform a simple math function. This app will satisfy a repetitive task wherein the user

相关标签:
3条回答
  • 2021-01-26 22:35

    The most basic way to do this would be to simply reset your EditText views. If you have logic that drives the update of these fields, then resetting them to an empty String and requesting a "recalculation" to update the hints.

    Something like this:

    private void onClear()
    {
        EditText firstField = (EditText)this.findById(R.id.firstField);
        EditText secondField = (EditText)this.findById(R.id.secondField);
        //...etc...
        if (firstField != null) firstField.setText("");
        if (secondField != null) secondField.setText("");
    
        updateHints();
    }
    
    private void updateHints()
    {
        //Logic for your "hints"
    }
    
    0 讨论(0)
  • 2021-01-26 22:40

    No, you have to implement it yourself. I suggest you create an int array with the IDs of all your EditTexts you want to reset (R.id.xyz). Then create a loop to .setText() to each of the EditTexts from the array, which you can call every time you want the fields to be cleared. Something like:

    private void resetFields() {
        EditText temp;
    
            for (int i = 0; i < myEditTexts.length; i++) {
                temp = (EditText) findViewById(textViews[i]);
            temp.setText("");
        }
    }
    
    0 讨论(0)
  • 2021-01-26 22:41

    My answer is just a better way but it still a kind of hard code. Anyone could find another way faster, please share.

    protected void clearForm() {
        Button btnClear = (Button) findViewById(R.id.btn_clear_text);
        btnClear.setOnClickListener(new View.OnClickListener() {            
            @Override
            public void onClick(View v) {
    
                EditText temp;      
                final int[] txtId = new int[] {
                    R.id.txt_s_free_word,
                    R.id.txt_s_property_name,
                    R.id.txt_s_ad_expense_from,
                    R.id.txt_s_ad_expense_to,
                    R.id.txt_s_station
                };
    
                for (int i = 0; i < txtId.length; i++) {
                    temp = (EditText) findViewById(txtId[i]);
                    temp.setText(null);
                }
            }
        });
    }
    
    0 讨论(0)
提交回复
热议问题