android studio 轮询修改对象属性(修改多个textview的text)

android studio版本:23.3.1

例程名称:loopsettextviewtext

这几天在帮一个朋友做个数独小游戏,如下图:

要可以玩自定义游戏,点击清空后,所以数字要清除。

要把所有的内容清空,一共有81个textview,要是一条条写可累死了,于是想到了轮询访问的方法。这个方法应用场景可能不多,但要是用起来不会也麻烦。

下面是解决方法:

1.定义一个数组:

        textViews = new TextView[3];
        textViews[0]=findViewById(R.id.textView1);
        textViews[1]=findViewById(R.id.textView2);
        textViews[2]=findViewById(R.id.textView3);

2.定义按钮点击事件:

public void chaneText(View view) {
        for (TextView textView : textViews) {
            textView.setText("2");

        }
    }

此事件要在xml里指定:

 <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="107dp"
        android:layout_marginTop="18dp"
        android:text="Button"
        android:onClick="chaneText"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

全部代码:

xml: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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.347"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.355" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="126dp"
        android:layout_marginTop="213dp"
        android:text="TextView"
        android:textAlignment="center"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="126dp"
        android:layout_marginTop="24dp"
        android:text="TextView"
        android:textAlignment="center"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="107dp"
        android:layout_marginTop="18dp"
        android:text="Button"
        android:onClick="chaneText"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

</androidx.constraintlayout.widget.ConstraintLayout>

mainactivity.java

package com.shudu.loopsettextviewtext;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {
    private TextView[] textViews;
    private TextView textview1;
    private TextView textview2;
    private TextView textview3;
    private Button button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        textview1 = (TextView) findViewById(R.id.textView1);
        textview2 = (TextView) findViewById(R.id.textView2);
        textview3 = (TextView) findViewById(R.id.textView3);
        button1 = (Button) findViewById(R.id.button1);
        textViews = new TextView[3];
        textViews[0]=findViewById(R.id.textView1);
        textViews[1]=findViewById(R.id.textView2);
        textViews[2]=findViewById(R.id.textView3);

    }
    public void chaneText(View view) {
        for (TextView textView : textViews) {
            textView.setText("搞定");

        }
    }
}

结果示意:

Android Studio 轮询中释放 WebView,可以在 `onPause()` 方法中暂停 WebView 的渲染和 JavaScript 执行,并且将 WebView 从布局中移除,在 `onResume()` 方法中重新加载 WebView。具体的实现方式如下: ```java public class MainActivity extends AppCompatActivity { private WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebView = findViewById(R.id.webView); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.loadUrl("https://www.google.com"); } @Override protected void onResume() { super.onResume(); if (mWebView != null) { mWebView.onResume(); mWebView.resumeTimers(); mWebView.setVisibility(View.VISIBLE); } } @Override protected void onPause() { super.onPause(); if (mWebView != null) { mWebView.onPause(); mWebView.pauseTimers(); mWebView.setVisibility(View.GONE); } } @Override protected void onDestroy() { super.onDestroy(); if (mWebView != null) { mWebView.loadUrl("about:blank"); mWebView.stopLoading(); mWebView.setWebChromeClient(null); mWebView.setWebViewClient(null); mWebView.destroy(); } } } ``` 在上述代码中,当 Activity 进入后台时,调用 `onPause()` 方法暂停 WebView 的渲染和 JavaScript 执行,并且将 WebView 从布局中移除;当 Activity 进入前台时,调用 `onResume()` 方法恢复 WebView 的渲染和 JavaScript 执行,并且将 WebView 加载到布局中。 在 Activity 销毁时,调用 `onDestroy()` 方法释放 WebView 相关的资源,包括停止 WebView 加载、清除 WebView 中的缓存和历史记录、销毁 WebView 等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kim5659

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值