我想创建一种方法,可以清除EditText
或TextView
的文字。
如何才能将其传递给任何类型的视图并清除?
这在Android中可行吗?
package com.androidapps.x90.battleexchange;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
//declare variables
double arrowBattleRate = 55.1;
double swordBattleRate = 100.9;
double amountEntered;
double convertedValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText currencyValue = (EditText) findViewById(R.id.txtCurrency);
final RadioButton swordsToArrows = (RadioButton) findViewById(R.id.radSwordsToArrows);
final RadioButton arrowsToSwords = (RadioButton) findViewById(R.id.radArrowToSwords);
final TextView battleResult = (TextView) findViewById(R.id.txtResult);
Button convert = (Button) findViewById(R.id.btnConvert);
convert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
amountEntered = Double.parseDouble(magicValue.getText().toString());
DecimalFormat tenth = new DecimalFormat("#.#");
if(arrowsToSwords.isChecked()) {
clearValuesFromControls(battleResult);
convertedValue = amountEntered * arrowBattleRate;
battleResult.setText(tenth.format(convertedValue) + " Swords.");
}
if(swordsToArrows.isChecked()){
convertedValue = amountEntered * swordsBattleRate;
battleResult.setText(tenth.format(convertedValue) + " Arrows.");
}
}
});
}
//pass in either EditText or TextView control
public void clearValuesFromControls(control) {
control.setText("");
}
}
答案 0 :(得分:4)
EditText派生自TextView,因此只需传入TextView。