[英]setText to TextView in Android Studio is not working
按下按鈕后,我需要根據 wifi 的信號強度更改 TextView 內容。 這是一些代碼:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
wifiUpdate();
}
});
}
protected void wifiUpdate() {
final TextView t1 = (TextView) findViewById(R.id.DataDisplay);
String temp = "Starting";
t1.setText(temp);
//some other code, where I using setText to assign variable, so it cant be static
}
在 activity_main.xml 我有:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/DataDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
tools:layout_editor_absoluteX="161dp"
tools:layout_editor_absoluteY="490dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
當我單擊按鈕時 - 它一直說“Hello world”。 任何幫助將不勝感激。
是的,while 循環是導致 UI 不更新的原因,不能在 UI 線程上進行更新。 將其移至 AsyncTask。
嘗試
t1.setText( "" + temp);
public class MainActivity extends AppCompatActivity {
Button button;
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button_id);
t1 = (TextView) findViewById(R.id.DataDisplay);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
wifiUpdate();
}
});
}
protected void wifiUpdate() {
String temp = "Starting";
t1.setText(""+temp);
//some other code, where I using setText to assign variable, so it cant be static
}
}
活動_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/DataDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
tools:layout_editor_absoluteX="161dp"
tools:layout_editor_absoluteY="490dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
你的代碼工作正常,我已經測試過了。如果這不起作用,請清理項目並重新構建,嘗試在 Android Studio 中執行無效緩存/從文件重新啟動。
另外你可以做的是從 XML 中的 TEXTVIEW 中刪除文本標簽,即使使用文本標簽也不會有任何問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.