How do I fix this mistake

Asked

Viewed 59 times

-3

I’m new in programming for android, I know java and c#, I want to press the button and change the label text only managed to appear on Toast

    package gabriel.meaperte;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import static android.widget.TextView.*;

public class MainActivity extends AppCompatActivity;

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

    public void exibirmensagem(View view){

        Toast.makeText(MainActivity.this, "Você apertou o botão", Toast.LENGTH_LONG).show();

    }

}

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Aperte o botão"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.174"
    app:layout_constraintHorizontal_bias="0.502"
    android:id="@+id/textView" />

<Button
    android:id="@+id/button"
    android:layout_width="159dp"
    android:layout_height="85dp"
    android:text="Botão"
    android:onClick="exibirmensagem"
    android:textSize="24sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    app:layout_constraintVertical_bias="0.55"
    app:layout_constraintHorizontal_bias="0.502" />

  • What error is occurring?

  • Report the error, so that a response compatible with your problem can be posted....

1 answer

1


You need to locate the view by its ID within its layout, after which set the text you need. See google documentation

Follow your code as an example:

package gabriel.meaperte;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import static android.widget.TextView.*;

public class MainActivity extends AppCompatActivity;

    private TextView textView;

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

        // Localiza a View através do seu id
        textView = (TextView) findViewById(R.id.textView);
    }

    public void exibirmensagem(View view){
        // Altera o valor da View      
        textView.setText("Seu novo valor no label");
        Toast.makeText(MainActivity.this, "Você apertou o botão", Toast.LENGTH_LONG).show();

    }

}
  • It worked, thanks, Have some good booklet to learn android, I have knowledge of c# and java

  • In the link I posted above https://developer.android.com/training/index.html. has a training, has enough content to get started.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.