在 android SetText 中只需要 resId [英] In android SetText only takes resId

查看:36
本文介绍了在 android SetText 中只需要 resId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能非常简单,但我在 android 开发中遇到了 settext 问题.

This is probably super simple but I am having problems with the settext in android development.

以下代码:

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

countDownTextView.SetText ("New text");

给出错误:

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

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

现在 settext 应该是 (string) 但出于某种原因,当我编写它时,它需要 int resid.如何仅使用 settext 和字符串更改 Textview?

Now settext should be (string) but for some reason when I write it, it wants int resid. How do I change the Textview with just settext and a string?

我曾尝试使用

string value ="new text"

但这也没有用.

做了一个更简单的版本,但遇到同样的问题

Made a simpler version, but get the same problem

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

和 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>

推荐答案

如果您使用的是 Xamarin:

If you are using Xamarin:

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

countDownTextView.Text = "value";

这篇关于在 android SetText 中只需要 resId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆