2014-01-18 7 views
0

Я использую Xamarin и у меня есть TextView загружены через файл макета 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 не отображается на экране вообще.

Могу ли я, пожалуйста, помочь?

Заранее спасибо.

+1

Я думаю, вы должны получить доступ к 'TextView' с идентификатором не' Layout', так что вы должны изменить '(TextView) FindViewById (Resource.Layout.TextView)' в '(TextView) FindViewById (Resource.id .TextView); 'и создать' Textview' с 'TextView id' в' TextView layout' –

+0

возможный дубликат [Uisng XML-макета для TextView] (http://stackoverflow.com/questions/21200507/uisng-xml-layout- for-textview) –

ответ

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

этот вопрос для C# 'xamarin' не родной java, пожалуйста, учтите, что ваш код работал на java android –

1

Объявление TextView в вашем xml. Retrive его помощью из Ид и SetText в Java

PhoneNumber.setText("This is a phone number: 0800 64 64 64"); 
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(); 

    } 
} 
    } 
Смежные вопросы