0

私は Mono For Android を初めて使用し、1 つの入力テキスト データを activity1.cs の 1 つの EditText から別のアクティビティの TextView に送信しようとしていますが、機能しません。コードは次のとおりです。

これは 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;
    }

他のアクティビティの EditText は、Activity1.cs の EditText からテキストを取得していません。

ありがとう!

4

3 に答える 3

0

あなたはそれをインテントに通すべきです

あなたの最初の活動で

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

あなたの2番目の活動でonCreate

String text = getIntent().getStringExtra("edit_text_content");
myEditText.setText(text);
于 2012-10-26T15:27:59.880 に答える
0

Activityあなたの問題は、 usingのインスタンスを作成できると仮定することですnew...

Activity1 ac1 = new Activity1();

...これをやろうとしないでください。うまくいきません。その結果、この行...

itemsList.Text = ac1.Item;

...別のデータにアクセスするための有効な方法ではありませんActivity

これを行う正しい方法は、2 番目の を開始するために使用される でextraキャリーとしてデータを渡すことです。IntentActivity

私は Mono を使用してプログラミングしていません。元は C# プログラマーですが、代わりに Java を学ぶことにしました (いずれにせよ、それらは非常に似ています)。

(私の頭の中で) Java で行う方法から Mono で行う方法に変換し、次のようなことを試してください...

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

次に、2番目Activityに、次のようなものonCreate(...)...

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

私が言ったように、私は Mono プログラマーではないので、メソッドの構文が正しくないかもしれませんが、データActivityを別のデータに渡す方法を示してくれることを願っています。

于 2012-10-26T15:28:23.653 に答える
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;
    }
}

私を助けてくれてありがとう!

于 2012-10-27T11:51:31.103 に答える