2012-10-26 2 views
0

나는 Mono를 처음 사용합니다. Android의 경우 하나의 입력 텍스트 데이터를 activity1.cs의 한 EditText에서 다른 활동의 TextView로 보내려고하지만, 작동하지 않습니다. 하나의 활동에서 다른 활동으로 EditText 데이터 보내기

가 Activity1.cs입니다 : 여기 코드입니다

public string Item; 

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

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

     // Get our button from the layout resource, 
     // and attach an event to it 
     Button AddButton = FindViewById<Button>(Resource.Id.AddButton); 
     Button ViewButton = FindViewById<Button>(Resource.Id.ViewButton); 
     EditText addNewAssignmentField = FindViewById<EditText>(Resource.Id.addNewAssignmentField); 


     AddButton.Click += delegate 
     { 
      if (addNewAssignmentField.Text == "") 
      { 
       Toast.MakeText(this, "Please Write Assignment To Add", ToastLength.Short).Show(); 
      } 
      else 
      { 
       Item = addNewAssignmentField.Text;//.ToString(); 
       Toast.MakeText(this, "Assignment Added!", ToastLength.Short).Show(); 
       addNewAssignmentField.Text = ""; 

      ShowMessage(Item); //ignore this 

       } 


     }; 

     ViewButton.Click += delegate 
     { 
      StartActivity(typeof(ViewListActivity)); 
     }; 
    } 

이 다른 활동 :

글고 다른 활동에 글고에서 텍스트를 받고 밤은
 Activity1 ac1 = new Activity1(); 

    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 
     // Set our view from the "ListLayout" layout resource 
     SetContentView(Resource.Layout.ListLayout); 
     Button button1 = FindViewById<Button>(Resource.Id.button1); 
     var listItemsTxt = new TextView(this); 
     EditText itemsList = FindViewById<EditText>(Resource.Id.itemsList); 
     itemsList.Text = ac1.Item; 
    } 

Activity1.cs에

감사합니다!

+1

나는 모노에 대해 모르지만 그 방법은 의도를 통해 이루어 지므로 "Android 용 모노 인 텐트"로 Google에 제안하십시오. – Budius

답변

0

당신은 두 번째 활동의 onCreate

String text = getIntent().getStringExtra("edit_text_content"); 
myEditText.setText(text); 
+0

감사합니다! 약간의 변화와 함께 마침내 성공했습니다! – LStyle

0

에 문제점을 첫 번째 활동

Intent intent = new Intent(Activity1.this, Activity2.class); 
intent.putStringExtra("edit_text_content", myEditText.getText().toString()); 
startActivity(intent); 

에서 의도

을 통해

그것을 통과해야하는 것은 당신이의 인스턴스를 만들 수 있다는 가정에있다 Activity을 사용하여 new ...

Activity1 ac1 = new Activity1(); 

... 절대 시도하지 마십시오. 작동하지 않습니다. 결과적으로이 라인은 ...

... 다른 Activity의 데이터에 액세스하는 올바른 방법은 아닙니다.

올바른 방법은 두 번째 Activity을 시작하는 데 사용되는 Intent에있는 extra으로 데이터를 전달하는 것입니다.

원래는 C# 프로그래머 임에도 불구하고 모노를 사용하여 프로그램하지 않고 대신 Java를 배우기로 결심했습니다. 어쨌든 그들은 매우 비슷합니다. 이 모노에서 수행되는 방법,이 같은 시도에 나는 자바에서 그것을 할 거라고 방법에서 (내 머리에) 번역

... 같은 두 번째 Activity에서 다음

Intent i = new Intent(); 
i.SetClass(this, typeof(ViewListActivity)); 
i.PutExtra("TheItem", item); 
StartActivity(i); 

, 뭔가 내가 말했듯이 나는 방법 올바른 구문이 없을 수 있지만 그 다른 한 Activity에서 데이터를 전달하는 방법을 보여줍니다 희망 그래서 나는 모노 프로그래머,

protected override void OnCreate(Bundle bundle) 
{ 
    ... 
    Intent i = GetIntent(); 
    itemsList.Text = i.GetStringExtra("TheItem"); 
} 

... onCreate(...)에 아니에요 다음과 같습니다.

+0

고마워요! 하지만 두 번째 활동에서 ive는 문자열을 사용하고 효과가있었습니다. – LStyle

+0

@LStyle : 음, 그래. 그래서 Mono C# 구문을 사용하여 답을주었습니다. (비록 Mono로 프로그래밍하지는 않지만) 완벽하게 작동 했어야 만하지만 Java 구문으로 작성된 대답을 받아 들였습니다. 한숨! – Squonk

+0

당신의 권리는, 비록 내가 couldnt 할 일을하고, 그래서 나는 다른 방법을 시도하고 그것은 효과가있다. 지금 당신의 방법을 시도해 보겠습니다, 고마워요! – LStyle

0

마지막으로 성공했습니다. 이 다른 새로운 사용자의 작업 코드입니다 : 활동 1.cs :

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

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

     // Get our button from the layout resource, 
     // and attach an event to it 
     Button button = FindViewById<Button>(Resource.Id.sendBtn); 
     EditText editText = FindViewById<EditText>(Resource.Id.editText1); 
     button.Click += delegate 
     { 
      string username = editText.Text.ToString(); 
      Intent intent = new Intent(); 
      intent.SetClass(this, typeof(Activity2)); 
      intent.PutExtra("username", editText.Text); 
      StartActivity(intent); 
     }; 
    } 

Activity2.cs을 :

public class Activity2 : Activity 
{ 
    string itemContent; 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 
     SetContentView(Resource.Layout.layout1); 
     var textView1 = new TextView(this); 
     EditText editTxt = FindViewById<EditText>(Resource.Id.editTxt); 
     itemContent = Intent.GetStringExtra("username"); 
     editTxt.Text = itemContent; 
    } 
} 

이 저를 도와 주셔서 모두 감사합니다!

관련 문제