Print variable in java

Asked

Viewed 78 times

0

excuse ignorance, but I am learning alone and reading a lot, I am at the beginning, I need to generate a password by calculating day * month * year, I created the variable that calculates this but I cannot make it appear, I left a string where it should appear!

    package com.roberto.senhauniplus;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

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

    public void texto(View view){

        TextView texto = findViewById(R.id.gerarsenha);
        texto.setText(R.id.gerarsenha);
        texto.setText("quero que a senha apareça aqui"); // aqui onde o botão substitui o texto pela senha!
        int senha = dia*mes*ano; // aqui onde calcula (ainda não testei)


    }
    Date dataAtual = new Date();

    int dia = dataAtual.getDate();
    int mes = dataAtual.getMonth();
    int ano = dataAtual.getYear();

}

I am using android studio, have a button where generates password working right, really just need to print the password!

2 answers

0

System.out.println("Your text is inserted here, between double quotes");

In the case of your variable is System.out.println(password);

0

Welcome to Stackoverflow, you just need to put the password variable in your Textview... to do so remove texto.setText(R.id.gerarsenha); and put texto.setText(senha);

Would look like this:

public void texto(View view){
    TextView texto = findViewById(R.id.gerarsenha);
    int senha = dia*mes*ano; // aqui onde calcula (ainda não testei)
    texto.setText(senha);
}

I would advise to initialize Textview inside onCreate, you do not need to always initialize it in the button action.

A more organized way:

public class MainActivity extends AppCompatActivity {
    private Date dataAtual;
    private int dia, mes, ano;
    private TextView texto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Inicializa as variáveis
        dataAtual = new Date();
        dia = dataAtual.getDate();
        mes = dataAtual.getMonth();
        ano = dataAtual.getYear();
        texto = findViewById(R.id.gerarsenha);
    }

    public void texto(View view) {
        int senha = dia * mes * ano; // aqui onde calcula (ainda não testei)
        texto.setText(senha);
    }
}

Browser other questions tagged

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