gpt4 book ai didi

java - 在 Android 中单击按钮时增大和减小文本大小

转载 作者:行者123 更新时间:2023-12-02 03:53:59 25 4
gpt4 key购买 nike

我是初学者。

我想在单击按钮时增加 TextView 的文本大小,并在单击另一个按钮时减小文本大小。

但问题是,它只是增加,文本大小并没有减小。

我使用 getTextSize() 方法获取文本大小。

这是我的 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=".textSize">

<Button
android:id="@+id/up"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="124dp"
android:text=" Size Up"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/down"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/down"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="124dp"
android:text=" Size down"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/up" />

<TextView
android:id="@+id/myTextView"
android:layout_width="328dp"
android:layout_height="58dp"
android:layout_marginStart="8dp"
android:layout_marginTop="216dp"
android:layout_marginEnd="8dp"
android:text="TextView"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="@+id/down"
app:layout_constraintStart_toStartOf="@+id/up"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是 Java 代码:

package com.example.toast;

import androidx.appcompat.app.AppCompatActivity;

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

public class textSize extends AppCompatActivity {
TextView textView;
Button upButton, downButton;
Float textSize,diff=2.0f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_size);

upButton=findViewById(R.id.up);
downButton= findViewById(R.id.down);
textView=findViewById(R.id.myTextView);

upButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textSize=textView.getTextSize();
textSize = textSize+diff;
textView.setTextSize(textSize);

}
});
downButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textSize=textView.getTextSize();
textSize = textSize-diff;
textView.setTextSize(textSize);

}
});

}
}

请告诉我哪里出错了。

最佳答案

无需设置和使用 textsize 类变量,只需执行以下操作:

textView.setTextSize(textView.getTextSize() + diff)

textView.setTextSize(textView.getTextSize() - diff)

关于java - 在 Android 中单击按钮时增大和减小文本大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56761444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com