2016-06-02 4 views
0

MainActivity 클래스와 클래스를 가지고 있는데 버튼 누름에 대한 모든 기능을 유지하려는 경우 mainActivity에서 버튼 클래스의 메소드를 사용하는 것만 볼 수 있습니다. 수업. 이것은 내가 지금까지 가지고있는 것입니다. 그것은 모두가 주요 활동 수업에 밀렸을 때 작동하지만 나는 이것을 피하고 싶습니다. ButtonMainActivityView 내에 있다고 가정xamarin의 주요 활동에서 클래스 사용

MainActivity 클래스

namespace Exercise3 
    { 
    [Activity(Label = "Exercise3", MainLauncher = true, Icon = "@drawable/icon")] 

public class MainActivity : Activity 
{ 
    btnClickClass btnClick = new btnClickClass(); 

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

     // Set our view from the "main" layout resource 
     SetContentView(Resource.Layout.Main); 

     //check if button is clicked 
     btnClick.ClickBtn(); 
    } 
} 
} 

버튼 클래스

 namespace Exercise3 
     { 
     public class btnClickClass : MainActivity 
     { 

     //run when the button is clicked 
     public void ClickBtn() 
     { 
      Button send = FindViewById<Button>(Resource.Id.sendButton); 
      send.Click += (object sender, EventArgs e) => { sendBtnClick(sender, e); }; 
     } 

     //button click functionality 
     public void sendBtnClick(object sender, EventArgs e) 
     { 
      bool emptyMessage = false; 

      EditText recipient = FindViewById<EditText>(Resource.Id.email); 
      EditText subject = FindViewById<EditText>(Resource.Id.subject); 
      EditText message = FindViewById<EditText>(Resource.Id.message); 

      //get the fields from the ID 
      var email = new Intent(Android.Content.Intent.ActionSend); 
      email.PutExtra(Android.Content.Intent.ExtraEmail, new string[] { recipient.Text }); 
      email.PutExtra(Android.Content.Intent.ExtraSubject, subject.Text); 
      email.PutExtra(Android.Content.Intent.ExtraText, message.Text); 
      email.SetType("message/rfc822"); 

      //check that the email has substance 
      if (message.Text == "" || subject.Text == "" || recipient.Text == "") 
      { 
       var dialog = new AlertDialog.Builder(this); 
       dialog.SetTitle("Error"); 
       dialog.SetMessage("You must supply a recipient, subject and a message"); 
       dialog.SetNeutralButton("OK", (s, a) => { }); 
       dialog.Show(); 
       emptyMessage = true; 
      } 

      //if message is not empty start email activity 
      if (!emptyMessage) 
      { 
       StartActivity(email); 
      } 
      //throw new NotImplementedException(); 
     } 
    } 
} 
+2

나는 당신이 올바른 길을 가고 있다고 생각하지 않는다. 당신의 활동을 상속받는 클래스를 만들 필요가 없다. –

+0

MainActivity.cs 파일에서 클릭 이벤트를 작성하십시오. 목록에 데이터를 표시하려면 클래스를 사용하여 listitem 및 데이터를 정의 할 수 있습니다. 기타 : https://www.youtube.com/watch?v=C4xodPCCmkU&index=9&list=PLCuRg51-gw5VqYchUekCqxUS9hEZkDf6l – Priya

+0

조각을 대신 사용해보십시오. https://developer.xamarin.com/guides/android/platform_features/fragments/fragments_walkthrough/ –

답변

0

,이 같은 MainActivitybtnClickClass의 코드를 움직일 수 :

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

    // Set our view from the "main" layout resource 
    SetContentView(Resource.Layout.Main); 

    Button send = FindViewById<Button>(Resource.Id.sendButton); 
    send.Click += (object sender, EventArgs e) => { sendBtnClick(sender, e); }; 
} 

이 이동 방법 후 sendBtnClick ~ MainActivity까지 사용할 수 있습니다.

관련 문제