简体   繁体   English

我想在android studio中更改文本颜色

[英]I want to change the text Color in android studio

I had a problem I didn't know how to change colors in Java 我有一个问题,我不知道如何在Java中更改颜色
I want to change the text color At the end of the code in the Calculation result The app is an application that tries to calculate body mass(bmi) I don't know what codes to use to change the color of writing this is my java code and xml code can I get help plz 我想更改文本颜色在计算结果中代码的末尾该应用程序是一个尝试计算体重(bmi)的应用程序,我不知道使用什么代码来更改书写颜色,这是我的java代码和xml代码可以向我寻求帮助吗

java code Java代码

package com.example.bmicalculator;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


//Main activity class start here
public class MainActivity extends Activity {

    //Define layout
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

// Get the references to the widgets
        final EditText e1 = (EditText) findViewById(R.id.et1);
        final EditText e2 = (EditText) findViewById(R.id.et2);
        final TextView tv4 = (TextView) findViewById(R.id.tv4);

        findViewById(R.id.ib1).setOnClickListener(new View.OnClickListener() {

            // Logic for validation, input can't be empty
            @Override
            public void onClick(View v) {

                String str1 = e1.getText().toString();
                String str2 = e2.getText().toString();

                if(TextUtils.isEmpty(str1)){
                    e1.setError("Please enter your weight");
                    e1.requestFocus();
                    return;
                }

                if(TextUtils.isEmpty(str2)){
                    e2.setError("Please enter your height");
                    e2.requestFocus();
                    return;
                }

//Get the user values from the widget reference
                float weight = Float.parseFloat(str1);
                float height = Float.parseFloat(str2)/1;

//Calculate BMI value
                float bmiValue = calculateBMI(weight, height);

//Define the meaning of the bmi value
                String bmiInterpretation = interpretBMI(bmiValue);

                tv4.setText(String.valueOf(bmiValue + "-" + bmiInterpretation));

            }
        });

    }

    //Calculate BMI
    private float calculateBMI (float weight, float height)
    {
        return (float) (weight / (height * height));
    }

    // her I want change color
    private String interpretBMI(float bmiValue)
    {

        if (bmiValue < 16) {
            return "Severely underweight" ;


        } else if (bmiValue < 18.5) {

            return "Underweight" ;
        } else if (bmiValue < 25) {

            return "Normal";
        } else if (bmiValue < 30) {

            return "Overweight";
        } else {
            return "Obese";
        }
    }

    }

xml code xml代码

<!-- Linear layout start here -->
<LinearLayout 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/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/pic"
    tools:context=".MainActivity">
    <!-- Text view for BMI Text -->

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="187dp">

        <TextView
            android:id="@+id/tv1"
            android:layout_width="305dp"
            android:layout_height="93dp"
            android:layout_gravity="center"
            android:layout_marginStart="53dp"
            android:layout_marginLeft="53dp"
            android:layout_marginEnd="53dp"
            android:layout_marginRight="53dp"
            android:layout_marginBottom="30dp"
            android:fontFamily="@font/sss"
            android:paddingLeft="15dp"
            android:paddingTop="40dp"
            android:shadowColor="@android:color/black"
            android:shadowDx="4"
            android:shadowDy="4"
            android:text="1_MAC_Community"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@android:color/white"
            android:textSize="25sp"
            android:typeface="serif"
            app:layout_constraintBottom_toTopOf="@+id/textView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="MissingConstraints" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="64dp"
            android:layout_marginLeft="64dp"
            android:layout_marginTop="30dp"
            android:layout_marginEnd="33dp"
            android:layout_marginRight="33dp"
            android:text="plz Fill YOUR  information"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@android:color/white"
            android:textSize="25sp"
            android:typeface="serif"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tv1"
            tools:ignore="MissingConstraints" />

    </android.support.constraint.ConstraintLayout>

    <TextView
        android:id="@+id/tv9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:paddingTop="30dp"
        android:text="WEIGHT (KG)"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/white"
        android:textStyle="bold|italic"
        android:typeface="serif" />

    <!-- Edit text for entering weight with hint in kgs -->
    <EditText
        android:id="@+id/et1"
        android:layout_width="314dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ems="10"
        android:fadingEdgeLength="10dp"
        android:hint="IN KGs"
        android:inputType="numberDecimal"
        android:scrollbarTrackHorizontal="@android:color/background_dark"
        android:textAlignment="center"
        android:textColorHighlight="#000000"
        android:textColorLink="#00FFFFFF"
        android:textCursorDrawable="@android:color/background_dark">

        <requestFocus />
    </EditText>

    <!-- Text view for HEIGHT(CM)text -->
    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:paddingTop="30dp"
        android:text="HEIGHT (Meter)"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/white"
        android:textStyle="bold|italic"
        android:typeface="serif" />

    <!-- Edit text for entering height with hint in cm -->
    <EditText
        android:id="@+id/et2"
        android:layout_width="281dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ems="10"
        android:hint="IN Meter"
        android:textAlignment="center"
        android:inputType="numberDecimal"></EditText>

    <!-- Button for calculating the formula, when pressed, with calculate written over it -->
    <Button
        android:id="@+id/ib1"
        android:layout_width="158dp"
        android:layout_height="51dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:fadingEdge="vertical"
        android:longClickable="true"
        android:nextFocusRight="@color/colorPrimaryDark"
        android:text="Calculate MY BMI"
        android:visibility="visible" />

    <!-- Text view for showing result -->
    <TextView
        android:id="@+id/tv4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:paddingTop="20dp"
        android:text=""
        android:textSize="20dp"
        android:textStyle="bold"
        android:textColor="@android:color/holo_orange_dark"/>

</LinearLayout>
    <!-- Linear layout ends here -->

Quite simple 非常简单

tv4.setTextColor(Color.RED);

Or if you have a speific colour resource 或者如果您有特定的颜色资源

tv4.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.your_colour));

tv4.setTextColor(getResources().getColor(R.color.your_colour));

Update: 更新:

Decare your view elements globally outside of onCreate onCreate以外的全局范围内取消视图元素

public class MainActivity extends Activity {

private EditText e1;
private EditText e2;
private TextView tv4;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...
    e1 = (EditText) findViewById(R.id.et1);
    e2 = (EditText) findViewById(R.id.et2);
    tv4 = (TextView) findViewById(R.id.tv4);
}

Its Quite easy 它很容易

1)Find Reference to Text View of which you want to change color 1)找到要更改颜色的文本视图的引用

2)Use Method 2)使用方法

MytextView.setColor(Color.BLUE);

Or if you have defined your custom color in colors.xml file 或者,如果您在colors.xml文件中定义了自定义颜色

MytextView.setColor(getResourses().getColor(R.color.Mycolor));

first remove final keyword from your TextView Object 首先从您的TextView对象中删除最终关键字

this code 此代码

final TextView tv4 = (TextView) findViewById(R.id.tv4);

Replace 更换

TextView tv4 = (TextView) findViewById(R.id.tv4);

then you can set color 然后你可以设置颜色

you can write hex code 你可以写十六进制代码

tv4.setTextColor("#ffffff");

or yuu can write color name 或你可以写颜色的名字

tv4.setTextColor(Color.WHITE);

or you can call it from color.xml file 或者您可以从color.xml文件中调用它

tv4.setTextColor(getResources().getColor(R.color.your_colour));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM