如何更改用户输入的编辑文本的颜色?

How can I change the color of my edit text which is entered by user?

我已经创建了一个注册页面并且我 select 提示颜色为蓝色现在我想更改在编辑文本中输入的文本的颜色请告诉我如何更改文本的颜色

这是我的 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=".Signup"
android:background="@color/purple_500">

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="660dp"
    android:layout_marginTop="80dp"
    android:background="@drawable/linear_round"
    android:orientation="vertical"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

</LinearLayout>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:text="Please Signup..."
    android:textColor="@color/white"
    android:textStyle="bold"
    android:textSize="29sp"
    android:gravity="center"
    android:layout_marginTop="16dp"
    android:id="@+id/textView">
</TextView>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:textColor="@color/black"
    android:hint="Enter your name"
    android:layout_marginTop="250dp"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:gravity="center"
    android:textColorHint="@color/purple_500"
    android:id="@+id/name"/>

<EditText
    android:id="@+id/pass"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:gravity="center"
    android:hint="Enter your password"
    android:inputType="textPassword"
    android:textColor="@color/black"
    android:textColorHint="@color/purple_500"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/name" />

<EditText
    android:id="@+id/email"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:gravity="center"
    android:hint="Enter your valid e-mail"
    android:inputType="textEmailAddress"
    android:textColor="@color/black"
    android:textColorHint="@color/purple_500"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/pass" />

<EditText
    android:id="@+id/number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:gravity="center"
    android:hint="Enter your cell number"
    android:inputType="phone"
    android:textColor="@color/black"
    android:textColorHint="@color/purple_500"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/email" />

<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/submit"
    android:layout_width="445dp"
    android:layout_height="163dp"
    android:layout_centerInParent="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/number"
    android:layout_marginBottom="100dp"
    app:lottie_autoPlay="true"
    app:lottie_loop="true"
    app:lottie_rawRes="@raw/submit" />

<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/trial"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_centerInParent="true"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/submit"
    app:lottie_autoPlay="true"
    app:lottie_loop="true"
    app:lottie_rawRes="@raw/trial" />

<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/signup_animation"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_centerInParent="true"
    app:layout_constraintBottom_toTopOf="@+id/name"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="70dp"
    app:lottie_autoPlay="true"
    app:lottie_loop="true"
    app:lottie_rawRes="@raw/signup" />
</androidx.constraintlayout.widget.ConstraintLayout>

如何在我的文本视图中添加选取框效果?我可以让文本视图从右向左滚动吗?

textColor 属性更改用户输入的文本的颜色。

android:textColor="@color/black"

HorizontalScrollView有一个chid视图,可以水平滚动


移动视图的方法有很多种。

使用类似关键字搜索 move view animation androidmove view programmatically android

如果TextView填满整个宽度你需要添加这些代码行

<TextView
    .
    .
    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    />

也不要忘记为 TexrtView 设置选择如下

textview.setSelected(true);

如果 TextView 没有填充宽度,唯一的方法就是调用此方法并将 TextView 传递给它。

Kotlin

    fun addMarquee(textView: TextView) {
    textView.viewTreeObserver.addOnGlobalLayoutListener(object :
        ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            val pixels = textView.measuredWidth - 1
            val params = textView.layoutParams
            params.width = pixels
            textView.layoutParams = params
            textView.isSelected = true
            textView.ellipsize = TextUtils.TruncateAt.MARQUEE
            textView.isSingleLine = true
            textView.marqueeRepeatLimit = -1
            textView.viewTreeObserver.removeOnGlobalLayoutListener(this)
        }
    })
}

Java

public void addMarquee(TextView textView) {
    textView.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            int pixels = textView.getMeasuredWidth() - 1;
            ViewGroup.LayoutParams params = textView.getLayoutParams();
            params.width = pixels;
            textView.setLayoutParams(params);
            textView.setSelected(true);
            textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
            textView.setSingleLine(true);
            textView.setMarqueeRepeatLimit(-1);
            textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });
}

注意: 此方法仅在 textView 宽度为 wrap_content.

时有效