2014-01-18 153 views
0

我使用Xamarin和我已經通過用下面的代碼的XML配置文件加載一個TextView:更改一個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 id'創建'Textview' –

+0

[用於TextView的Uisng XML佈局]的可能重複(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

在你的xml中聲明一個TextView。 從ID和的setText在Java中使用Retrive它

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(); 

    } 
} 
    }