SetText for Android only requires resId

This is probably very simple, but I have problems with settext in android development.

The following code:

TextView countDownTextView = FindViewById<TextView> (Resource.Id.countdownTimer);

countDownTextView.SetText ("New text");

gives errors:

The best overloaded method match for 'Android.Widget.TextView.SetText(int)' has some invalid arguments

Argument 1: cannot convert from 'string' to 'int'

Now the settext should be (string), but for some reason, when I write it, it wants the int to remain. How to change a textview using only settext and string?

I tried to use

string value ="new text"

but this also does not work.

Made a simpler version but got the same issue

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace CountdownTest
{
    [Activity (Label = "CountdownTest", MainLauncher = true)]
    public class MainActivity : Activity
    {
    int count = 1;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button> (Resource.Id.myButton);

        button.Click += delegate {
            button.Text = string.Format ("{0} clicks!", count++);
        };

        TextView textV = (TextView)FindViewById<TextView> (Resource.Id.CallText);

        textV.SetText ("diverse");
    }
}
}

And XML

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/myButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <TextView
        android:text="Text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/CallText" />
</LinearLayout>
+4
source share
4 answers

If you are using Xamarin:

TextView countDownTextView = FindViewById<TextView> (Resource.Id.countdownTimer);

countDownTextView.Text = "value";
+6
source

Consider this code

String value = "New Text";
TextView countDownTextView = (TextView)findViewById(R.id.countdownTimer);
countDownTextView.SetText (value);
0
source

:

TextView countDownTextView = (TextView) findViewById( R.id.countdownTimer );
countDownTextView.setText( "New text" );

.

You get the wrong component and, in addition, you call the methods wrong. The letter "F" on findViewById should not be capitalized, but tiny. The same goes for the resource identifier and inside the parentheses.

0
source

just change it and try

    TextView countDownTextView = (TextView)findViewById (R.Id.countdownTimer); 

    countDownTextView.SetText("New text");
0
source

Source: https://habr.com/ru/post/1544161/


All Articles