我有一个非常有趣的问题。让我强调一下,我没有为Android编程很多,似乎我可能已经偏离了某个地方。
我的问题是触发事件onActivityResult()。我已经阅读了Xamarin收据但不知何故它不起作用。该例程不会在MainActivity中调用。
我甚至试图在java上做一个类似的例子,它就像魅力一样。
所以,现在去做生意。我有一个MainActivity,可以启动配置活动。此活动应该从服务器收集IP(用户手动将其放入),并将String返回到MainActivity。公平而简单。
MainActivity代码段:
[Activity(Label = "Test", MainLauncher = true, Icon = "@drawable/ax")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
setBindigs();
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
switch (resultCode)
{
case Result.Ok:
//do something
break;
}
}
private void setBindigs()
{
ImageView ax = FindViewById<ImageView>(Resource.Id.ax);
ax.Click += HandleImageClick;
}
private void HandleImageClick(object sender, EventArgs e)
{
var conf = new Intent(this, typeof(Configuration));
StartActivityForResult(conf, Convert.ToInt32(Result.Ok));
}
配置代码段:
[Activity(Label = "Configuration")]
public class Configuration : Activity
{
private Button[] Connects;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Configuration);
setBindigs();
}
private void setBindigs()
{
const int KeyboardLength = 2;
Connects = new Button[KeyboardLength];
for (int I = 0; I < KeyboardLength; ++I)
{
Connects[I] = FindViewById<Button>(Resource.Id.ConnectButton + I);
Connects[I].Click += HandleKeyboardClick;
}
}
private void HandleKeyboardClick(object sender, EventArgs e)
{
Button Clicked = sender as Button;
Result ActivityResult = Result.Ok;
String IpText = String.Empty;
switch(Clicked.Id)
{
case Resource.Id.ConnectButton:
EditText IP = FindViewById<EditText>(Resource.Id.ServerIP);
IpText = IP.Text;
break;
case Resource.Id.DisconnectButton:
ActivityResult = Result.Canceled;
break;
}
Intent myIntent = new Intent (this, typeof(MainActivity));
myIntent.PutExtra ("IP", IpText);
SetResult (Result.Ok, myIntent);
Finish();
}
你有什么想法吗?在java中,我使用选项getIntent();
而不是Intent myIntent = new Intent (this, typeof(MainActivity));
答案 0 :(得分:2)
我刚刚在代码中发现了问题。为了将来参考,这里似乎是问题所在:
调用StartActivityForResult()
时,我使用了参数StartActivityForResult(conf, Convert.ToInt32(Result.Ok));
。似乎预期结果不应该作为Convert.ToInt32(Result.Ok)
传递,而应该传递为0.
这解决了我的问题