android - 将 TextView 设置为分贝强度的字符串 - Android Studio

标签 android variables textview set

问题:

  • 将id为“readOut”的textView设置为测试wifi信号强度的值

代码:

class wifi {
    int signalStrength = 0;
    int loopToggle = 0;
    Context context = MainActivity.this;

    @RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void loop() throws InterruptedException {
        while (loopToggle == 0) {
            WifiManager signalStrength = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            String wifiInfo = WifiManager.EXTRA_WIFI_INFO;
            TextView textView = (TextView) textView.findViewById(readOut);
            Thread.sleep(1000);
        }
    }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.wifilocator.MainActivity">

    <TextView
        android:id="@+id/readOut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="filler"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

感谢您提供的任何帮助!

最佳答案

如果您引用具有 xml 属性 android:id="@+id/readOut"的 View,则必须编写 R.id.readOut

所以整行应该是

TextView textView = (TextView) textView.findViewById(R.id.readOut);

R 代表 R将生成的类包含您在应用程序中需要的各种资源。

关于android - 将 TextView 设置为分贝强度的字符串 - Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49658526/

相关文章:

php - var_dump() 和 print_r() 都不会显示可读信息....但同样令人困惑的输出。为什么?

java - 如何通过 XML 将图像添加到 Android TextView

android - 在Android布局xml文件中将TextView的首字母大写

Java Android 完成 Activity ,开始上一个 Activity

android - 使用 HttpClient 的 Android httppost 到 https URL 安全吗?

c - C 中的 %g 和 %f 有什么区别?

variables - Pascal-如何将实数转换为整数变量

android - TextView.setAllCaps() 等同于 14 岁以上的 API?

android - 在较旧的 Android 平台上使用 cz.msebera.android.httpclient 时防止 SSL_do_handshake 失败

android - 如何通过 Android SDK 获得 root 权限?