简体   繁体   English

Android - 将文本设置为 TextView

[英]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 .我目前正在为学校项目学习一些 android,但我无法弄清楚将文本动态设置为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编辑:这是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...不工作是指显示的文本在任何时候都不会改变......

In your layout XML:在您的布局 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");

PUT THIS CODE IN YOUR XML FILE将此代码放入您的 XML 文件中

<TextView
    android:id="@+id/textview1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

PUT THIS CODE IN YOUR JAVA FILE将此代码放入您的 Java 文件中

// Declaring components like TextView globally is a good habit // 全局声明TextView之类的组件是个好习惯

TextView mTextView = (TextView) findViewById(R.id.textview1);

// Put this in OnCreate // 把它放在 OnCreate 中

mTextView.setText("Welcome to Dynamic TextView");

Well, @+id/listaVista ListView is drawn after @+id/texto and on top of it.那么, @+id/listaVista的ListView之后得出@+id/texto和在它上面。 So change in ListView from:因此在 ListView 中从以下更改:

android:layout_below="@+id/editText1"

to:到:

android:layout_above="@+id/texto"

Also, since the list is drawn after textview, I find it dangerous to have android:layout_alignRight="@+id/listaVista" in TextView.此外,由于列表是在 textview 之后绘制的,我发现在 TextView 中有android:layout_alignRight="@+id/listaVista"很危险。 So remove it and find another way of aligning.因此,将其移除并找到另一种对齐方式。

EDIT Taking a second look at your layout I think this is what you really want to have:编辑再看看你的布局,我认为这就是你真正想要的:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".EnviarMensaje" >

    <TextView
        android:id="@+id/textView1"
        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" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:ems="10"
        android:inputType="text" />

    <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:text="TextView" />

    <ListView
        android:id="@+id/listaVista"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/texto"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1" >
    </ListView>

</RelativeLayout>

In layout file.在布局文件中。

<TextView
   android:id="@+id/myTextView"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="Some Text"
   android:textSize="18sp"
   android:textColor="#000000"/>

In Activity活动中

TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setText("Hello World!");

In xml use this:在 xml 中使用这个:

<TextView 
       android:id="@+id/textView"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>

In Activity define the view:在 Activity 中定义视图:

Textview textView=(TextView)findViewById(R.id.textView);
textView.setText(getResources().getString(R.string.txt_hello));

In string file:在字符串文件中:

<string name="txt_hello">Hello</string>

Output: Hello输出:你好

I had a similar problem.我有一个类似的问题。 It turns out I had two TextView objects with the same ID.原来我有两个具有相同 ID 的 TextView 对象。 They were in different view files and so Eclipse did not give me an error.它们位于不同的视图文件中,因此 Eclipse 没有给我错误。 Try to rename your id in the TextView and see if that does not fix your problem.尝试在 TextView 中重命名您的 id,看看这是否不能解决您的问题。

为什么不尝试将 textview 内容分配给 onStart() 而不是 onCreate()

in your activity_main.xml paste this code:在您的 activity_main.xml 中粘贴此代码:

            <TextView
                 android:id="@+id/name"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentLeft="true"
                 android:layout_alignParentTop="true"
                 android:layout_marginLeft="19dp"
                 android:layout_marginTop="43dp"
                 android:text="@string/name" />

and go to res folder->values->strings.xml paste the below code with the code that already exists:并转到 res folder->values->strings.xml 将以下代码粘贴到已存在的代码中:

          <string name="name">Escriba su mensaje y luego seleccione el canal.</string>

the above code means that you have created a textview with id:name(android:id="@+id/name") and assigned that textview to a string with an identifier name(android:text="@string/name") in strings.xml your using that identifier name to assign the text,上面的代码意味着您已经创建了一个带有 id:name(android:id="@+id/name") 的 textview 并将该 textview 分配给一个带有标识符 name(android:text="@string/name") 的字符串在 strings.xml 中,您使用该标识符名称来分配文本,

Try This:尝试这个:

TextView err = (TextView)findViewById(R.id.text);

Ensure you import TextView.确保导入 TextView。

这应该可以解决问题:

TextView.setText("test");

Please correct the following line:请更正以下行:

err = (TextView)findViewById(R.id.texto);

to:到:

err = (TextView)findViewById(R.id.err);

In XML file,在 XML 文件中,

<TextView
       android:id="@+id/textview"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="My Name"
       android:textColor="#cccccc"/>

In Java Activity file,在 Java 活动文件中,

public class MainActivity1 extends Activity
 {
   TextView t1;
   public void onCreate(Bundle onSavedInstance)
      {
         setContentView(R.layout.xmlfilename);
          t1 = (TextView)findViewbyId(R.id.textview);
      }
 }

first your should create an object for text view TextView show_alter首先你应该为文本视图TextView show_alter创建一个对象

show_alert = (TextView)findViewById(R.id.show_alert);
show_alert.setText("My Awesome Text");

You can set string in textview programatically like below.您可以像下面那样以编程方式在 textview 中设置字符串。

TextView textView = (TextView)findViewById(R.id.texto);
err.setText("Escriba su mensaje y luego seleccione el canal.");

or或者

TextView textView = (TextView)findViewById(R.id.texto);
err.setText(getActivity().getResource().getString(R.string.seleccione_canal));

You can set string in xml like below.您可以在 xml 中设置字符串,如下所示。

<TextView
         android:id="@+id/name"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"
         android:layout_marginLeft="19dp"
         android:layout_marginTop="43dp"
         android:text="Escriba su mensaje y luego seleccione el canal." />

or或者

<TextView
         android:id="@+id/name"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"
         android:layout_marginLeft="19dp"
         android:layout_marginTop="43dp"
         android:text="@string/seleccione_canal" />

As you have given static text正如你给出的静态文本

err.setText("Escriba su mensaje y luego seleccione el canal.");

It will not change , it will remain same.它不会改变,它将保持不变。

Example for Dynamic Text for textview is : textview 的动态文本示例是:

MainActivity.java
package com.example.dynamictextview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    int count = 0;
    Button clickMeBtn;
    TextView dynamicText;

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

        clickMeBtn = (Button) findViewById(R.id.button_click);
        dynamicText = (TextView) findViewById(R.id.textview);

        clickMeBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                count++;
                dynamicText.setText("dynamic text example : " + count);

            }
        });

    }

}

For activity_main.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="match_parent"
    tools:context="com.example.dynamictextview.MainActivity" >

    <Button 
        android:id="@+id/button_click"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click me"
        android:layout_centerInParent="true" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:layout_below="@id/button_click"
        android:layout_centerHorizontal="true"
        android:textSize="25sp"
        android:layout_marginTop="20dp"
        />

</RelativeLayout>

You should use ButterKnife Library http://jakewharton.github.io/butterknife/您应该使用 ButterKnife 库http://jakewharton.github.io/butterknife/

And use it like并使用它

@InjectView(R.id.texto)
TextView err;

in onCreate method在 onCreate 方法中

ButterKnife.inject(this)
err.setText("Escriba su mensaje y luego seleccione el canal.");

Your code is ok, you are loading the .xml that contains the TextView using setContentView() :您的代码没问题,您正在使用setContentView()加载包含TextView的 .xml :

   setContentView(R.layout.activity_enviar_mensaje);

and then getting the reference of the TextView inside activity_enviar_mensaje.xml , and setting a text:然后在activity_enviar_mensaje.xml获取 TextView 的引用,并设置文本:

err = (TextView)findViewById(R.id.texto);
err.setText("Escriba su mensaje y luego seleccione el canal.");

The problem is that your TextView is hidden by the ListView :问题是你的 TextView 被ListView隐藏了:

在此处输入图片说明

I know its 2 months but yeah我知道它是 2 个月,但是是的

replace your code替换你的代码

Private TextView err;
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.");
}

In your layout.在您的布局中。 Your Texto should not contain (android:text=...).您的 Texto 不应包含 (android:text=...)。 I would remove this line.我会删除这一行。 Either keep the Java string OR the (android:text=...)保留 Java 字符串或 (android:text=...)

转到您的 activityMain 并通过从小部件部分添加小部件并通过选择和键入手动更改文本来设置文本。

final TextView err = (TextView)findViewById(R.id.texto);
err.setText("Escriba su mensaje y luego seleccione el canal.");

you can find every thing you need about textviewhere你可以在这里找到你需要的关于 textview 的所有东西

In your layout XML you can insert 您可以在布局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");

Kotlin Solution科特林解决方案

To set using a String, just use this要使用字符串设置,只需使用此

view.text = "My string"

To do the same with a resource value, add this extension property to much more easily set your text要对资源值执行相同操作,请添加此扩展属性以更轻松地设置文本

view.textRes = R.string.my_string

var TextView.textRes
    get() = 0 // HACK: property requires getter
    set(@StringRes textRes) {
        text = resources.getText(textRes)
    }

也许您已经在 onResume() 函数中分配了文本

It is so easy to add dynamic text in TextView. 在TextView中添加动态文本非常容易。 There is setText method in android with the help of this method you can set dynamic text for example.. android中有setText方法,借助此方法,您可以设置动态文本。

textView.setText("Hello World");

for complete explanation you can visit this link. 有关完整的说明,请访问此链接。 https://www.owlbuddy.com/learn/TextView-In-Android https://www.owlbuddy.com/learn/TextView-In-Android

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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