将字符串加载到textview中

时间:2015-07-23 20:35:44

标签: c# android string xamarin textview

我需要将一个字符串加载到按钮单击事件后显示在新页面上的textview中。单击按钮时,java.lang.runtimeexception会一直抛出。 “java.lang.reflect.InvocationTargetException”

以下是我的代码部分:

SetContentView (Resource.Layout.Main);
        Button button1 = FindViewById<Button>(Resource.Id.story1);
        button1.Click += delegate {
            SetContentView (Resource.Layout.next1);
            TextView textvtest = (TextView) FindViewById(Resource.Id.textView1);
            textvtest.Text = "test";
        };

3 个答案:

答案 0 :(得分:2)

从第一个活动开始,按钮点击:

 button1.Click += delegate {
       var activity2 = new Intent (this, typeof(Activity2));
       activity2.PutExtra ("MyData", "test");
       StartActivity (activity2);
 };

在第二项活动中,onCreate()

protected override void OnCreate (Bundle bundle)
        {
        SetContentView (Resource.Layout.next1);
        TextView textvtest = (TextView) FindViewById(Resource.Id.textView1);

        string text = Intent.GetStringExtra ("MyData") ?? "Data not available";
        textvtest.Text = text;

        }

答案 1 :(得分:0)

我不知道这是否重要,但使用C#设置文字视图的其他示例包括

(object sender, EventArgs e)

delegate

delegate(object sender, EventArgs e)

答案 2 :(得分:0)

你的代码有点不对,试试这个

SetContentView (Resource.Layout.Main);
Button button1 = FindViewById<Button>(Resource.Id.story1);
button1.Click += delegate {
    SetContentView (Resource.Layout.next1);
    TextView textvtest = FindViewById<TextView>(Resource.Id.textView1);
    textvtest.Text = "test";
};