EditText 未正确更改
EditText not changing correctly
我是 Whosebug 的新手。
我正在尝试制作一个单位转换器,其中函数 "calculateCurrency" returns 结果的 Double 值。但是 TextView "outputNumber" 的值总是根据先前的输入而变化。
Ex- 如果我键入 1,则 outputNumber 更改为 "null" 但是当我在 1 之后输入另一个数字时,outputNumber 更改为它应该在 1 上更改的值。但是Toast 显示正确的值,即在 1 上显示 1,因此 myInputNumber 的值根据输入是正确的。
谁能帮帮我?
inputNumber.addTextChangedListener(
new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
try {
myInputNumber = Double.parseDouble(s.toString());
Double myText = calculations.calculateCurrency(myInputNumber, convertFrom, convertTo);
Toast.makeText(MainActivity.this, myInputNumber+"", Toast.LENGTH_SHORT).show();
outputNumber.setText(myText+"");
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
}
);
calculations.calculateCurrency的代码是:
public Double calculateCurrency(Double input, String convertFrom, String convertTo) throws ExecutionException, InterruptedException {
myCurrencyFetcher fetchCurrency = new myCurrencyFetcher();
fetchCurrency.execute(Double.toString(input), convertFrom, convertTo);
return myText;
}
class myCurrencyFetcher extends AsyncTask<String, Double, Double>{
URL url;
StringBuffer stringBuffer;
@Override
protected Double doInBackground(String... params) {
try{
Double myInputNumber = Double.parseDouble(params[0]);
String convertFrom = params[1];
String convertTo = params[2];
url = new URL("http://api.fixer.io/latest?base="+convertFrom+"&symbols="+convertTo);
HttpURLConnection connection;
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
stringBuffer = new StringBuffer();
String line = "";
while((line = reader.readLine()) != null){
stringBuffer.append(line);
}
String finalJSON = stringBuffer.toString();
JSONObject parentObject = new JSONObject(finalJSON);
JSONObject finalObject = parentObject.getJSONObject("rates");
String rate = finalObject.getString(convertTo);
//String year = finalObject.getString("year");
Double myRate = Double.parseDouble(rate);
return myInputNumber*myRate;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Double result) {
super.onPostExecute(result);
Calculations.myText = result;
}
}
再次尝试在方法 calculations.calculateCurrency(myInputNumber, convertFrom, convertTo);
中添加 synchronized
您必须更改密码。
@Override
public void afterTextChanged(Editable s) {
try {
myInputNumber = Double.parseDouble(s.toString());
Double myText = calculations.calculateCurrency(myInputNumber, convertFrom, convertTo);
Toast.makeText(MainActivity.this, myInputNumber+"", Toast.LENGTH_SHORT).show();
//outputNumber.setText(myText+""); // Remove this line
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
将 TextView (outputNumber) 作为参数添加到构造函数。
myCurrencyFetcher fetchCurrency = new myCurrencyFetcher(outputNumber);
class myCurrencyFetcher extends AsyncTask<String, Double, Double>{
private WeakReference<TextView> weakOutputNumber;
public myCurrencyFetcher (TextView outputNumber) {
weakOutputNumber= new WeakReference<TextView>(outputNumber);
}
/* ... Other methods are the same */
@Override
protected void onPostExecute(Double result) {
super.onPostExecute(result);
//Calculations.myText = result;
if (weakOutputNumber != null) {
TextView outputNumber = weakOutputNumber.get();
if (outputNumber != null) {
outputNumber.setText(new String(result));
}
}
}
}
我是 Whosebug 的新手。
我正在尝试制作一个单位转换器,其中函数 "calculateCurrency" returns 结果的 Double 值。但是 TextView "outputNumber" 的值总是根据先前的输入而变化。
Ex- 如果我键入 1,则 outputNumber 更改为 "null" 但是当我在 1 之后输入另一个数字时,outputNumber 更改为它应该在 1 上更改的值。但是Toast 显示正确的值,即在 1 上显示 1,因此 myInputNumber 的值根据输入是正确的。
谁能帮帮我?
inputNumber.addTextChangedListener(
new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
try {
myInputNumber = Double.parseDouble(s.toString());
Double myText = calculations.calculateCurrency(myInputNumber, convertFrom, convertTo);
Toast.makeText(MainActivity.this, myInputNumber+"", Toast.LENGTH_SHORT).show();
outputNumber.setText(myText+"");
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
}
);
calculations.calculateCurrency的代码是:
public Double calculateCurrency(Double input, String convertFrom, String convertTo) throws ExecutionException, InterruptedException {
myCurrencyFetcher fetchCurrency = new myCurrencyFetcher();
fetchCurrency.execute(Double.toString(input), convertFrom, convertTo);
return myText;
}
class myCurrencyFetcher extends AsyncTask<String, Double, Double>{
URL url;
StringBuffer stringBuffer;
@Override
protected Double doInBackground(String... params) {
try{
Double myInputNumber = Double.parseDouble(params[0]);
String convertFrom = params[1];
String convertTo = params[2];
url = new URL("http://api.fixer.io/latest?base="+convertFrom+"&symbols="+convertTo);
HttpURLConnection connection;
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
stringBuffer = new StringBuffer();
String line = "";
while((line = reader.readLine()) != null){
stringBuffer.append(line);
}
String finalJSON = stringBuffer.toString();
JSONObject parentObject = new JSONObject(finalJSON);
JSONObject finalObject = parentObject.getJSONObject("rates");
String rate = finalObject.getString(convertTo);
//String year = finalObject.getString("year");
Double myRate = Double.parseDouble(rate);
return myInputNumber*myRate;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Double result) {
super.onPostExecute(result);
Calculations.myText = result;
}
}
再次尝试在方法 calculations.calculateCurrency(myInputNumber, convertFrom, convertTo);
synchronized
您必须更改密码。
@Override
public void afterTextChanged(Editable s) {
try {
myInputNumber = Double.parseDouble(s.toString());
Double myText = calculations.calculateCurrency(myInputNumber, convertFrom, convertTo);
Toast.makeText(MainActivity.this, myInputNumber+"", Toast.LENGTH_SHORT).show();
//outputNumber.setText(myText+""); // Remove this line
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
将 TextView (outputNumber) 作为参数添加到构造函数。 myCurrencyFetcher fetchCurrency = new myCurrencyFetcher(outputNumber);
class myCurrencyFetcher extends AsyncTask<String, Double, Double>{
private WeakReference<TextView> weakOutputNumber;
public myCurrencyFetcher (TextView outputNumber) {
weakOutputNumber= new WeakReference<TextView>(outputNumber);
}
/* ... Other methods are the same */
@Override
protected void onPostExecute(Double result) {
super.onPostExecute(result);
//Calculations.myText = result;
if (weakOutputNumber != null) {
TextView outputNumber = weakOutputNumber.get();
if (outputNumber != null) {
outputNumber.setText(new String(result));
}
}
}
}