简体   繁体   English

动态更改TextView的文本

[英]Dynamically changing the text of a TextView

I have a TextView that starts out with a default text value, and then based on what a user does, that TextView's text needs to change in the code when a button is clicked. 我有一个TextView,它以默认文本值开始,然后根据用户的操作,单击按钮时,TextView的文本需要在代码中进行更改。 Seems simple enough, yet I'm running into problems. 看起来很简单,但是我遇到了问题。

Currently, what is happening is when the user clicks the submit button which triggers the change of the text, the new text is just being added to the screen under the original TextView instead of just simply changing the text value. 当前,正在发生的情况是,当用户单击触发按钮以触发文本更改时,新文本只是被添加到原始TextView下的屏幕上,而不仅仅是简单地更改文本值。 It's almost as if it's adding a new TextView. 几乎就像是要添加一个新的TextView一样。

Here is the code that does this: 这是执行此操作的代码:

lblSlogan.Invalidate();
lblSlogan.SetText(currentSlogan.Slogan, TextView.BufferType.Normal);

I also tried it this way, with no luck: 我也这样尝试过,没有运气:

lblSlogan.Invalidate();
lblSlogan.Text = currentSlogan.Slogan;

lblSlogan is a TextView. lblSlogan是一个TextView。 Am I missing something? 我想念什么吗? I also tried it without the invalidate(), but that changed nothing either. 我也尝试了不使用invalidate()的情况,但这也没有改变。

Thanks. 谢谢。

-- edit -- -编辑-

It's important to note that I'm using C# with Xamarin. 请务必注意,我在Xamarin中使用C#。 Not Java. 不是Java。 Here is my click method for the button. 这是我点击按钮的方法。 This is where the TextView change happens. 这是发生TextView更改的地方。

btnOk.Click += delegate(object sender, EventArgs e)
      {
            if (answerBox.Text.ToLower() == currentSlogan.Company.ToLower())
            {
                // correct answer
                currentUserScore += currentSlogan.Points;
                currentSlogan.Answered = true;
                DatabaseBuffer.MarkSloganAnsweredAndUpdateScore(currentSlogan, currentUserScore);
                currentSlogan = DatabaseBuffer.GetNextUnansweredSlogan(currentSlogan.ID);
            }

            if (currentUserScore >= pointsToPass)
            {
                // user has beaten level
            }
            else
            {
                lblSlogan.SetText(currentSlogan.Slogan, TextView.BufferType.Normal);
                answerBox.Text = "";
            }
        };

i didn't understand why you are calling the method invalidate() on your TextView, otherwise ,a simple code like this should work (add this code in the onCreate() method) : 我不明白为什么要在TextView上调用方法invalidate() ,否则,这样的简单代码应该可以工作(在onCreate()方法中添加此代码):

setContentView(R.layout.main);
TextView lblSlogan = (TextView) findViewById(R.id.lblSlogan);
Button btnChangeSlogan = (Button) findViewById(R.id.btnChangeSlogan);

btnChangeSlogan.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
       lblSlogan.setText("Put your new text here");// cal setText() in the onclick method when ever you want to change the text 
   }
});

I think your problem is this: 我认为您的问题是这样的:

answerBox.Text.ToLower() == currentSlogan.Company.ToLower()

you should use "equals", not "==". 您应该使用“等于”,而不是“ ==“。

(answerBox.Text.ToLower()).Equals( currentSlogan.Company.ToLower())

A couple of points here. 这里有几点。

Personally I use the built in abstract methods that Xamarin provides. 我个人使用Xamarin提供的内置抽象方法。 They tend to give me much more consistent results. 他们倾向于给我更一致的结果。 You can simply assign the new value to the .Text property of the Textview. 您可以简单地将新值分配给Textview的.Text属性。 IE IE

textView.Text = newValue;

In C# you do not need to use the .Equals operator to do string comparisons. 在C#中,您不需要使用.Equals运算符进行字符串比较。 That's strictly a Java requirement. 严格来说,这是Java的要求。 See this [link] ( Why would you use String.Equals over ==? ). 参见此[link]( 为什么要使用String.Equals over ==? )。

Assign listener to button and in that listener you add text with the setText() method (or appendText() for appending..) 将侦听器分配给按钮,然后在该侦听器中使用setText()方法添加文本(或通过appendText()进行添加。)

findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            text.setText("This is new text");
        }
    });

Here you can add text View Dynamically. 您可以在此处添加“动态查看”文本。

var aLabel = new TextView (this);
aLabel.Text = "Hello Text!!!";
aLabel.SetTextSize (Android.Util.ComplexUnitType.Dip, 15f);
RelativeLayout ll = new RelativeLayout(this);
ll.AddView(aLabel);

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

相关问题 动态更改文本块中的文本 - dynamically changing text in textblock 在Windows 8中动态更改标题文本 - Changing the title text dynamically in Windows 8 动态更改Winforms ComboBox中的项目文本 - Dynamically changing the text of items in a Winforms ComboBox Silverlight-更改动态创建的按钮文本的颜色 - Silverlight - Changing button text color created dynamically 动态更改gridview列的标题文本 - Dynamically changing header text of a gridview column 是否可以在不更改文本长度的情况下在c#中动态调整标签的大小? - is it possible to dynamically resize the lable in c# without changing the length of text? 根据列值动态更改gridview的linkbutton的文本 - Changing the text of a linkbutton of a gridview dynamically on the basis of a column value 通过动态更改字体大小来调整按钮控件中的文本 - Fit the text in the button control on resize by changing Fontsize dynamically 有没有一种方法可以动态搜索TextView? (Xamarin) - Is there a way to search for a TextView dynamically? (Xamarin) 创建一种方法,该方法将以程序方式写出控制台文本,例如迷你游戏并动态更改静态文本 - Creating a method that will procedurally write out console text like a mini game & dynamically changing still-text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM