简体   繁体   中英

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. 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. It's almost as if it's adding a new 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. Am I missing something? I also tried it without the invalidate(), but that changed nothing either.

Thanks.

-- edit --

It's important to note that I'm using C# with Xamarin. Not Java. Here is my click method for the button. This is where the TextView change happens.

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) :

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. They tend to give me much more consistent results. You can simply assign the new value to the .Text property of the Textview. IE

textView.Text = newValue;

In C# you do not need to use the .Equals operator to do string comparisons. That's strictly a Java requirement. See this [link] ( Why would you use String.Equals over ==? ).

Assign listener to button and in that listener you add text with the setText() method (or appendText() for appending..)

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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