마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

혹시 Xamarin으로 안드로이드 앱 개발해보시는 분이 계신가요...

0 추천
여기다가 이런 질문 올려도 되는지는 모르겠지만,

일단 문제가된다면 바로 삭제하겠습니다.

Xamarin이 안드로이드 스튜디오랑 비슷한 부분이 좀 있어서 국내에 커뮤니티가 없기에

이렇게 질문글 올립니다 ㅠ

 

 

-------------------------------------------

--------------------------------------------- Main_Activity

-------------------------------------------

 

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Views;

namespace Project
{
    [Activity(Label = "Project", MainLauncher = true)]
    public class Main_Activity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            RequestWindowFeature(WindowFeatures.NoTitle);
            
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main_Layout);

            ImageButton _Back_Button = (ImageButton)FindViewById(Resource.Id._Back_Button);
            TextView _Title_Text = (TextView)FindViewById(Resource.Id._Title_Text);
            ImageButton _Exit_Button = (ImageButton)FindViewById(Resource.Id._Exit_Button);

            _Title_Text.Text = "Setting";

            FragmentTransaction _Setting_Content_Adatper = this.FragmentManager.BeginTransaction();
            Project.Fragments.Setting_Content_Fragment _Setting_Content = new Project.Fragments.Setting_Content_Fragment();
            _Setting_Content_Adatper.Replace(Resource.Id._Adapter_Content, _Setting_Content);
            _Setting_Content_Adatper.Commit();

            _Back_Button.Click += (s, e) =>
            {
                _Title_Text.Text = "Setting";

                FragmentTransaction _Main_Fragment = this.FragmentManager.BeginTransaction();
                Project.Fragments.Setting_Content_Fragment Setting_Main_Fragment = new Project.Fragments.Setting_Content_Fragment();
                _Main_Fragment.Replace(Resource.Id._Adapter_Content, Setting_Main_Fragment);
                _Main_Fragment.AddToBackStack(null);
                _Main_Fragment.Commit();
            };

            _Exit_Button.Click += (s, e) =>
            {
                Finish();
            };
        }
    }
}

 

 

 

-------------------------------------------

--------------------------------------------- Setting_Fragment

-------------------------------------------

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;

namespace Project.Fragments
{
    public class Setting_Content_Fragment : Fragment
    {
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your fragment here
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View _Setting_Content = inflater.Inflate(Resource.Layout.Setting_Content_Layout, container, false);

            Button _General_Button = (Button)_Setting_Content.FindViewById(Resource.Id._btnGeneral);

            LayoutInflater _Change = (LayoutInflater)Activity.GetSystemService(Context.LayoutInflaterService);
            View _Change_Text = _Change.Inflate(Resource.Layout.Main_Layout, null);
            TextView _Title_Text = (TextView)_Change_Text.FindViewById(Resource.Id._Title_Text);

            _General_Button.Click += (s, e) =>
            {
                _Title_Text.Text = "General";

                FragmentTransaction _Main_Fragment = this.FragmentManager.BeginTransaction();
                Project.Fragments.General_Content.General_Main_Fragment General_Main_Fragment = new Project.Fragments.General_Content.General_Main_Fragment();
                _Main_Fragment.Replace(Resource.Id._Adapter_Content, General_Main_Fragment);
                _Main_Fragment.Commit();
            };

            return _Setting_Content;
        }
        
    }
}

 

 

이런식으로 되어있는데요,

지금 Setting_Fragment에서 Main_Activity에 있는 TextView를 불러와서

Setting_Fragment에 Button을 눌렀을 때, TextView에 글이 바뀌게 하고 싶은데

안되더라구요..

어떻게 해야 좋을까요?ㅠ
Xamarin 님이 2018년 5월 15일 질문

1개의 답변

0 추천
 
채택된 답변
fragment 에서 activity textview를 불러들이지 말고 fragment에 콜백 인터페이스 하나 만들어서

activity 쪽에서 fragment 콜백 구현해주고 fragment쪽에서 버튼 누르면 콜백 이벤트함수 호출하는 방식이 더 좋지않을까용 ??
익명사용자 님이 2018년 5월 15일 답변
음 제가 아직 초보라 정확히 이해가 안가는데
어떻게 해야하는건가요?ㅠㅠ
fragment에다가 내부 인터페이스 클래스를 만들고 거기에 함수를 하나 작성한담에 그걸 activity 쪽에서 구현해주는거죠. 구현 내용은 activity안에 있는 textview를 설정해주는 거고요.
이를테면

A fragment.class{

...
public interface callback {
 public void onClick(String text);
}
...

private callback m_callback;


public void setCallBack(callback callback){
 m_callback = callback;
}

...
플래그먼트 버튼.setOnclick(
 m_callback.onClick("text");
}
...
}
이렇게 인터페이스랑 그거 객체 만들고 set해주는 함수 작성하신담에 버튼에다가는 눌리면 만들어준 콜백객체에서 함수를 호출해주고요
activity 에서

B activity class{
...
private Afragment.callback callback= new callback{

    @Override
    public void onClick(String text){
      텍스트뷰.setText(text);
    }
}


Afragment a = new Afragment();
a.setCallBack(callback);
...
}

}

저런식으로 프래그먼트 만들때 activity에서 구현해뒀던 인터페이스 객체를 set함수에 적용시켜주면 될것같아요.

이게 그냥 안드로이드 스튜디오 기준 소스라 xamarin 형식에 맞게 바꿔주시면 될거같아요
일단 안드로이드 스튜디오쪽으로는 이해를 했어요!
이제 Xamarin에 맞게 고치는게 문제겠군요.
정말 감사합니다!
...