关于android有时候Edittext.setText无效的解决方法,有效

textView.post(new Runnable() {
@Override
public void run() {
textView.setText(TAG+“用户可见的时候”);
}
});

你只要加上控件的post方法就可以及时刷新设置的值了,亲测有效

package com.example.ibm; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import com.google.gson.Gson; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class MainActivity extends AppCompatActivity { private EditText etHeight; private EditText etWeight; private Button btnCalculate; private TextView tvResult; private OkHttpClient client; private Gson gson; private static final String API_KEY = "d889e63ef7ad041b837dcc1b3ebc5a2b"; private static final String API_URL = "http://apis.juhe.cn/fapig/calculator/weight"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etHeight = findViewById(R.id.et_height); etWeight = findViewById(R.id.et_weight); btnCalculate = findViewById(R.id.btn_calculate); tvResult = findViewById(R.id.tv_result); client = new OkHttpClient(); gson = new Gson(); btnCalculate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String height = etHeight.getText().toString(); String weight = etWeight.getText().toString(); if (height.isEmpty() || weight.isEmpty()) { tvResult.setText("请输入身高和体重"); return; } float h = Float.parseFloat(height); float w = Float.parseFloat(weight); if (h <= 0 || w <= 0) { tvResult.setText("身高和体重必须大于0"); return; } calculateBMI(h, w); } }); }
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值