A simple doubt

Asked

Viewed 61 times

-1

Hello guys I just downloaded an android booklet and I’m doubtful about an exercise I will post the pictures of how you are in the booklet and how I did and I would like someone to explain to me why you are not recognizing the commands seeMessageButton and Onclicklistener:

workbook:inserir a descrição da imagem aqui inserir a descrição da imagem aqui

as is in android studio_layout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/name_label"
        />
        <EditText
            android:id="@+id/name_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    <Button
        android:id="@+id/see_message_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/see_message"
        />
    <TextView
        android:id="@+id/show_message_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="@string/hello_message"
        android:textStyle="bold"
        android:textSize="24dp"
        android:visibility="invisible"

        />



</LinearLayout>

as it is in android studio_layout_main code:

package com.example.pc_vicl.myapplication;

import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;

import java.awt.font.TextAttribute;

public class MainActivity extends AppCompatActivity {

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

        final EditText nameEditText=(EditText) findViewById(R.id.name_edit_text);
        seeMessageButton = (Button) findViewById(R.id.see_message_button);
        final TextView showMessageText = (TextView) findViewById(R.id.show_message_text);

        seeMessageButton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick (View view){
                String name=nameEditText.getEditableText().toString();
                showMessageText.setText(getString(R.string.hello_message, name));
                showMessageText.setVisibility(View.VISIBLE);

            }

        });

    }
}

how is in android studio_layout_strings:

<resources>
    <string name="name_label">Nome:</string>
    <string name="see_message">Ver mensagem</string>
    <string name="app_name">Ola Mundo </string>
    <string name="hello_message">Olá, %1$!</string>
</resources>
  • Add the codes directly here, editing your question, otherwise it is difficult to reproduce your code to find a solution. And explain better the problem you’re facing.

  • thanks for the tip

  • What mistake are you making? It was not very clear in the question. And explain what you are trying to do, just by codes can not know.

  • does not recognize the command seeMessageButton nen the Onclicklistener

  • Probably because it is final. Declare the EditText and the TextView as parametrics of the class.

  • forgive my ignorance but how can I do it ?

  • Forget, run again, no apparent error in code.

Show 2 more comments

1 answer

1

Is trying to use OnClickListener without doing the import, will have to add the import as follows:

import android.view.View.OnClickListener;

Already on line 14 of your code you missed specify the type at the time you declare seeMessageButton see:

Button seeMessageButton = (Button) findViewById(R.id.see_message_button);

I hope I’ve helped

Browser other questions tagged

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