简体   繁体   中英

Text view doesn't show after button click - Android

I have a problem with Text View and Button in Android Studio. I would like to update my text view when i clik a button but it doesn't work. This is my code:

activity_distance_to_run.xml

<?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=".MainActivity"
tools:layout_editor_absoluteY="25dp">


<TextView
    android:id="@+id/Distancerun2"
    android:layout_width="126dp"
    android:layout_height="50dp"
    android:layout_marginTop="50dp"
    android:text="Distance to ....."
    android:textSize="14sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/distancetorun"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="10dp"
    android:layout_marginRight="10dp"
    android:text="run"
    app:layout_constraintBaseline_toBaselineOf="@+id/distancetoride"
    app:layout_constraintEnd_toStartOf="@+id/distancetoride"
    app:layout_constraintStart_toStartOf="parent" />

<Button
    android:id="@+id/distancetoride"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="9dp"
    android:layout_marginRight="9dp"
    android:layout_marginTop="32dp"
    android:text="ride a bicycle"
    app:layout_constraintEnd_toStartOf="@+id/distancetoswim"
    app:layout_constraintStart_toEndOf="@+id/distancetorun"
    app:layout_constraintTop_toBottomOf="@+id/Distancerun2" />

<Button
    android:id="@+id/distancetoswim"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="swim"
    app:layout_constraintBaseline_toBaselineOf="@+id/distancetoride"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/distancetoride" />

<Button
    android:id="@+id/yes"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="32dp"
    android:layout_marginStart="32dp"
    android:text="I did it !!"
    app:layout_constraintBaseline_toBaselineOf="@+id/no"
    app:layout_constraintStart_toStartOf="parent" />

<Button
    android:id="@+id/no"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="116dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:text="I don't want to do it !!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView1" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="23dp"
    android:layout_marginEnd="25dp"
    android:layout_marginLeft="25dp"
    android:layout_marginRight="25dp"
    android:layout_marginStart="25dp"
    android:layout_marginTop="221dp"
    android:autoText="true"
    android:clickable="true"
    android:enabled="false"
    android:freezesText="false"
    android:text="Elo ziomek"
    android:textSize="24sp"
    app:layout_constraintBottom_toTopOf="@+id/no"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

distancetorun.java

package pl.agcode.sqliteexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class distance_to_run extends Activity {
Button distancetorun;
TextView txtView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_distance_to_run);
    distancetorun = (Button) findViewById(R.id.distancetorun);
    txtView = (TextView) findViewById(R.id.textView1);
    distancetorun.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            txtView.setText("NewText");
        }
        });
    }
}

Thanks for help I will try to repair it but don't know how. This is very important to repair this for me. :)

Looks all good except textview1 color.

Add below line for the textview1 in layout xml

 <TextView
    android:id="@+id/textView1"
    android:textColor="@android:color/black"

After launching the app

在此处输入图片说明

After clicking RUN button

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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