2017-05-05 57 views
-2

簡單的問題對於C#新手C#如果計數器如何更改按鈕「功能」,按它

using Android.App; 
using Android.Widget; 
using Android.OS; 

namespace Sukkis 
{ 
    [Activity(Label = "sukkis", MainLauncher = true, Icon = "@mipmap/icon")] 
    public class MainActivity : Activity 
    { 
     string person = "sukkis"; 
     string age = "24"; 
     string place = "hell"; 
     int count = 1; 

     protected override void OnCreate(Bundle savedInstanceState) 
     { 
      base.OnCreate(savedInstanceState); 

      // 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); 
      TextView kentta = FindViewById<TextView>(Resource.Id.tiedot); 

      if (count == 2) 
      { 
       button.Click += delegate { kentta.Text = "Info"; }; 
       button.Click += delegate { button.Text = "Click additional info"; }; 
       count--; 
      } 
      else 
      { 
       button.Click += delegate { kentta.Text = $"Name: {person}\nage: {age}\nPlace: {place} \n \nC# is fun"; }; 
       button.Click += delegate { button.Text = "Hide"; }; 
       count++; 
      } 
     } 
    } 
} 

如何解決這個功能之後?現在,當我第一次按下按鈕,但它不會在第二次按下後再次運行功能。如何實現,如果正確循環以改變每隔一個按鈕之後的文本?感謝您的回答。

回答

2

if聲明應該放在點擊處理程序中

button.Click += delegate { 
    if (count == 2) 
    { 
    kentta.Text = "Info"; 
    button.Text = "Click additional info"; 
    count--; 
    } else{ 
    kentta.Text = $"Name: {person}\nage: {age}\nPlace: {place} \n \nC# is fun"; }; 
    button.Text = "Hide"; 
    count++; 
    } 
} 
3

是更好,如果u使用絕對數字,而不是++ & - 試試這個:

if (count == 2) 
{ 
    button.Click += delegate { kentta.Text = "Info"; }; 
    button.Click += delegate { button.Text = "Click additional info"; }; 
    count = 1; 
} 
else if(count == 1) 
{ 
    button.Click += delegate { kentta.Text = $"Name: {person}\nage: {age}\nPlace: {place} \n \nC# is fun"; }; 
    button.Click += delegate { button.Text = "Hide"; }; 
    count = 2; 
} 
+1

在這種情況下,最好只使用布爾值 –

相關問題