2014-08-27 46 views
0

我是創建android應用程序的總新手。我可以在OnCreate方法中使用AXML中的對象,但是我不能在事件處理程序radioButton_Click中使用它們。我不想知道如何在事件處理程序radioButton_Click中使用它們。在OnCreate之外使用UI對象方法

protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 
     SetContentView(Resource.Layout.Main); 
     RadioButton radioButton1 = FindViewById<RadioButton>(Resource.Id.radioButton1); 
     RadioButton radioButton2 = FindViewById<RadioButton>(Resource.Id.radioButton2); 
     radioButton1.Click += radioButton_Click; 
     radioButton2.Click += radioButton_Click; 
     //radioButton1 and radioButton2 are recognized here. 
    } 

    void radioButton_Click(object sender, EventArgs e) 
    { 
     //radioButton1 and radioButton2 are not recognized here. 
     //I want to know how to use IN THIS METHOD the objects loaded from the AXML 
     // in OnCreate (the two radio buttons) 
    } 

回答

0

那麼,你可能必須創建這個變量作爲類成員變量。

RadioButton radioButton1; 
RadioButton radiobutton2; 
protected override void OnCreate(Bundle bundle) 
{ 
    base.OnCreate(bundle); 
    SetContentView(Resource.Layout.Main); 
    radioButton1 = FindViewById<RadioButton>(Resource.Id.radioButton1); 
    radioButton2 = FindViewById<RadioButton>(Resource.Id.radioButton2); 
    radioButton1.Click += radioButton_Click; 
    radioButton2.Click += radioButton_Click; 
    //radioButton1 and radioButton2 are recognized here. 
} 

void radioButton_Click(object sender, EventArgs e) 
{ 
    //radioButton1 and radioButton2 are not recognized here. 
    //I want to know how to use IN THIS METHOD the objects loaded from the AXML 
    // in OnCreate (the two radio buttons) 
} 
0

在你的活動中,創建RadioButtons的字段(最好是私人的)。 這將幫助您在全球範圍內使用它們。

通知通過FindViewById投下查找結果!

相關問題