2014-08-27 2 views
0

Im новичок в создании приложений для Android. Я могу использовать объекты из AXML внутри метода OnCreate, но я не могу использовать их внутри обработчика событий radioButton_Click.I не знаю, как их использовать в обработчике событий radioButton_Click.Использование объектов пользовательского интерфейса вне метода OnCreate

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!

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