我有2个活动(FirstActivity和SecondAcitivity) FirstActivity内容EditText和Button 我希望在用户单击按钮时销毁firstActivity并在EditText中输入时间后启动Secondactivity
答案 0 :(得分:0)
使用CountDownTimer:
String text = editText.getText().toString();
int time = Integer.parseInt(text);//in seconds
time = time * 1000;
new CountDownTimer(time, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
}
}.start();
将其放在onClick中,因此请实现View.OnClickListener并添加以下代码:
public void onClick(View view) {
if (view.getId() == R.id.button1) {
//countdown code
}
}
答案 1 :(得分:0)
Button myBtn = (Button)findViewById(R.id.YourBtnId);
final EditText myEditText = (EditText)findViewById(R.id.YourEditTextId);
myBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int time = Integer.parseInt(myEditText.getText().toString());
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent startActivity = new Intent(FirstActivity.this, SecondAcitivity.class);
startActivity(startActivity);
finish();
}
}, time * 1000);
}
});
答案 2 :(得分:0)
我猜你也有主动或家庭活动? 如果是这样,你可以做以下事情来得到想要的结果:
在你的第一个onCreate()方法中:
Button mButton = (Button)findViewById(R.id.TestButton);
final EditText mEditText = (EditText)findViewById(R.id.TestTextBox);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int time = Integer.parseInt(mEditText.getText().toString());
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("Time", time);
startActivity(intent);
}
});
比在mainactivity的onCreate()方法中放置以下内容:
if (getIntent().getIntExtra("Time", 0) > 0) {
int time = getIntent().getIntExtra("Time" , 0);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
finish();
}
}, time * 1000);
}
编辑:(如果您没有MainActivity)
首先:在XML中注册接收器
<receiver android:name=".AlarmReceiver"/>
创建AlarmReceiver类
package (your package)
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm time reached", Toast.LENGTH_SHORT).show();
Intent i = new Intent();
i.setClassName("(your package name)", "(your package name.SecondActivity)");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
在FirstActivity的onCreate方法中:
Button mButton = (Button)findViewById(R.id.TestButton);
final EditText mEditText = (EditText)findViewById(R.id.TestTextBox);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int time = Integer.parseInt(mEditText.getText().toString());
if(time > 0) {
Intent myIntent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, time);
alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(getApplicationContext(), "Starting Activity in: " + time + " seconds", Toast.LENGTH_SHORT).show();
finish();
}
}
});
希望这可以帮助你;)
PS:关于在活动之间传递图片,您可以尝试以下操作: 首先在活动顶部声明您的位图:
private Intent myIntent;
将以下代码添加到FirstActivity中的 onCreate 方法
myIntent = new Intent(FirstActivity.this, AlarmReceiver.class);
并在bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
以下添加以下内容:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
myIntent.putExtra("SendImage", byteArray);
并添加以下Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
ByteArrayOutputStream stream = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
myIntent.putExtra("SendImage", byteArray);
然后在你的AlarmReceiver
中byte[] mByteArray = intent.getByteArrayExtra("SendImage") //above Intent i = new Intent();
i.putExtra("Image", mByteArray); //above context.startActivity(i);
和你的SecondActivity
byte[] mByteArray= getIntent().getByteArrayExtra("Image");
if(mByteArray != null){
Bitmap mBitmap = BitmapFactory.decodeByteArray(mByteArray, 0, mByteArray.length);
mTestImage.setImageBitmap(mBitmap); //Your imageview
}
不要认为这是最好的解决方案,但我已对此进行了测试,并且确实有效;)