这可能非常简单,但我在 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/