2017-05-05 75 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

在这种情况下,最好只使用布尔值 –

相关问题