我正在使用Xamarin,我通过XML布局文件加载了TextView,其代码如下:
SetContentView (Resource.Layout.TextView);
在屏幕上加载TextView后,如何更改TextView的文本?如何通过代码引用TextView,因为我还没有将其声明为变量?
我试过没有结果:
TextView PhoneNumber = (TextView)FindViewById(Resource.Layout.TextView);
PhoneNumber.Text = "This is a phone number: 0800 64 64 64";
TextView根本不会显示在屏幕上。
我可以请一些帮助吗?
提前致谢。
答案 0 :(得分:2)
替换它:
SetContentView (Resource.Layout.TextView);
和
TextView PhoneNumber = (TextView)FindViewById(Resource.Layout.TextView);
PhoneNumber.Text = "This is a phone number: 0800 64 64 64";
用这个:
SetContentView (Resource.Layout.Your_Layout_Name);
和
TextView PhoneNumber = (TextView)FindViewById(Resource.id.TextView);
PhoneNumber.setText("This is a phone number: 0800 64 64 64");
答案 1 :(得分:1)
在xml中声明TextView。 使用java中的id和setText进行重复
PhoneNumber.setText("This is a phone number: 0800 64 64 64");
答案 2 :(得分:0)
同样的问题不会出现指定的值。
public class MainActivity : Activity
{
public string mess;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
StartListening ();
Button bt = FindViewById<Button>(Resource.Id.button1);
bt.Click += delegate { start();};
// Get our button from the layout resource,
// and attach an event to it
}
public void start()
{
TextView text = FindViewById<TextView> (Resource.Id.textView1);
StartListening();
text.Text = mess;
//text.SetText (mess);
}
private readonly UdpClient udp = new UdpClient(45000);
public void StartListening()
{
this.udp.BeginReceive(Receive, new object());
}
public void Receive(IAsyncResult ar)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 45000);
byte[] bytes = udp.EndReceive(ar, ref ip);
mess = Encoding.ASCII.GetString(bytes);
StartListening();
}
}
}