Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

Android - Set text to TextView

I'm currently learning some android for a school project and I can't figure out the way to set text dynamically to a TextView.

Here is my code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_enviar_mensaje);
    err = (TextView)findViewById(R.id.texto);
    err.setText("Escriba su mensaje y luego seleccione el canal.");
}

This is currently not working and I can't find a way to make it work...

Any help will be much appreciated... Thank you for the time, José.

EDIT: Here is the activity_enviar_mensaje.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ...
    tools:context=".EnviarMensaje" >
    ...
    <TextView
        android:id="@+id/texto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/listaVista"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/listaVista"
        android:text="TextView" />
    ...
</RelativeLayout>

By not working I mean the text shown does not change at any moment...

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your layout XML:

<TextView
        android:id="@+id/myAwesomeTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Escriba el mensaje y luego clickee el canal a ser enviado"
        android:textSize="20sp" />

Then, in your activity class:

// globally 
TextView myAwesomeTextView = (TextView)findViewById(R.id.myAwesomeTextView);

//in your OnCreate() method
myAwesomeTextView.setText("My Awesome Text");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...