我是xamarin
机器人的新手。
我有一个重置时间的问题
我想将时间传递给SeeTime
活动,并将时间从该活动重设为60,这也将重设MainActivity
中的时间。
public class MainActivity : AppCompatActivity
{
public EditText timer;
Button btn;
public int elapsedTime; // counting values integer
Handler myHandler; // used to delay runnable for a second which delay_RATE
int delay_RATE = 1000; //delay
Java.Lang.Runnable r;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
elapsedTime = 60; // default value
myHandler = new Handler(); //initializing handler
r = new Java.Lang.Runnable(Run);
timer = FindViewById<EditText>(Resource.Id.timertxt);
btn = FindViewById<Button>(Resource.Id.btn1);
btn.Click += Btn_Click;
Counter();
}
private void Btn_Click(object sender, System.EventArgs e)
{
elapsedTime = 20;
StartActivity(typeof(SeeTime));
}
void Counter()
{
elapsedTime--; // increment
timer.Text = elapsedTime.ToString();
myHandler.PostDelayed(r, delay_RATE);
if (elapsedTime == 0)
{
elapsedTime = 20;
}
}
void Run()
{ Counter(); }
SeeTime活动:
public class SeeTime : Activity
{
MainActivity mainActivity;
private EditText timer;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Time);
timer = FindViewById<EditText>(Resource.Id.txtTime);
mainActivity = new MainActivity();
timer.Text = mainActivity.timer.Text;
// Create your application here
}
}
答案 0 :(得分:0)
您可以尝试以下方法将elapsedTime
的{{1}}传递给MainActivity
活动。
// MainActivity
SeeTime
// SeeTime规则
var intent = new Intent(this, typeof(SeeTime));
intent.PutExtra("Timer", elapsedTime);
StartActivity(intent);
此外,当您从int intValue = Intent.Extras.GetInt("Timer", 20);
活动返回到SeeTime
时,可以使用MainActivity
方法将OnResume()
重置为60。有关详细信息:https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/activity-lifecycle/ < / p>
所以代码是这样的:
MainActivity
elapsedTime
SeeTime活动:
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Content;
namespace App80
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
public EditText timer;
Button btn;
public int elapsedTime; // counting values integer
Handler myHandler; // used to delay runnable for a second which delay_RATE
int delay_RATE = 1000; //delay
Java.Lang.Runnable r;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
elapsedTime = 60; // default value
myHandler = new Handler(); //initializing handler
r = new Java.Lang.Runnable(Run);
timer = FindViewById<EditText>(Resource.Id.timertxt);
btn = FindViewById<Button>(Resource.Id.btn1);
btn.Click += Btn_Click;
Counter();
}
protected override void OnResume()
{
base.OnResume();
elapsedTime = 60;
timer.Text = elapsedTime.ToString();
}
private void Btn_Click(object sender, System.EventArgs e)
{
var intent = new Intent(this, typeof(SeeTime));
intent.PutExtra("Timer", elapsedTime);
StartActivity(intent);
}
void Counter()
{
elapsedTime--; // increment
timer.Text = elapsedTime.ToString();
myHandler.PostDelayed(r, delay_RATE);
if (elapsedTime == 0)
{
elapsedTime = 20;
}
}
void Run()
{ Counter(); }
}
}