传递来自c#活动的输入值
本文关键字:输入 活动 | 更新日期: 2023-09-27 18:15:40
我试图将用户输入文本从activity1传递到activity2。我尝试了很多方法但是所有的方法都让我犯了这个错误,这是错误我得到当我试图把值在textview。
https://www.dropbox.com/s/cpsbflk4r1ep46x/error.PNG//this is the first activity
SetContentView(Resource.Layout.Main);
base.OnCreate(bundle);
TextView t1 = FindViewById<TextView>(Resource.Id.t1);
long l = 1;
ImageButton i1 = FindViewById<ImageButton>(Resource.Id.i1);
i1.Click += delegate
{
t1.Text = l++.ToString();
};
Button b1 = FindViewById<Button>(Resource.Id.b1);
b1.Click += (sender, e) =>
{
string b = t1.Text;
Intent intent = new Intent(this, typeof(Activity2));
intent.PutExtra("j", b);
StartActivity(intent);
};
//this is the second activity
TextView money = FindViewById<TextView>(Resource.Id.money);
SetContentView(Resource.Layout.layout1);
var l = this.Intent.Extras.GetString("j");
money.Text = l;
谢谢,
您可以使用getStringExtra(QUERY)
:
var l = Intent.getStringExtra("j");
如果你得到一个null并且你正在使用getStringExtra(QUERY)
。在PutExtra中使用的变量中添加.toString()
。
试试这个:
关于主活动
var activity2 = new Intent (this, typeof(next_activity));
activity2.PutExtra ("value",i);
StartActivity (activity2);
关于next_activity
var Passed_value = Intent.GetStringExtra ("value",0);
// for int use: Intent.GetIntExtra
也试着改变
t1.Text = l++.ToString();
t1.Text = l++;
和
string b = t1.Text;
string b = t1.Text.ToString();