2014-08-27 2 views
0

안드로이드 응용 프로그램을 만드는 데 초보자입니다. OnCreate 메서드 내에서 AXML의 개체를 사용할 수는 있지만 이벤트 처리기 radioButton_Click에서 사용할 수는 없습니다. 이벤트 처리기 radioButton_Click에서 사용하는 방법을 알지 못해도됩니다.외부에서 UI 개체 사용하기 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

활동 내에서, 라디오 버튼의 필드를 생성하십시오. 이 옵션을 사용하면 전역 범위에서 사용할 수 있습니다.

찾기 결과를 FindViewById!

관련 문제