android - 在 android 中 SetText 只需要 resId

标签 android xamarin

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

以下代码:

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?

我试过用

string value ="new text"

但这也没有用。

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

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:

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

countDownTextView.Text = "value";

关于android - 在 android 中 SetText 只需要 resId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24162405/

相关文章:

c# - 对前台 Activity 有一个静态引用很好吗?

java - 不能用java随机设置一个新按钮

Android 事件总线不适用于两个事件监听器

css - Xamarin.Forms - 像 CSS 一样的嵌套样式继承?

c# - Xamarin 在后面的代码中形成点击事件不等待 View 模型中的异步确认

android - 下载管理器无法下载大小超过 2 GB 的文件

android - iOS UIImagePNGRepresentation base64 编码未给出预期输出

xaml - 忽略绑定(bind)初始化

Xamarin.Forms,如何使用我当前的 Xamarin.Forms 找到兼容版本的 Android 支持库

c# - Xamarin Android 请求运行时权限不起作用