Fragment 跳转activity 并返回结果
理论部分
实现部分
1. Fragment 类实现 主要实现是红色部分
使用startActivityForResult(Intent,Activity.RESULT_FIRST_USER);跳转到activity。
重写onActivityResult(int requestCode, int resultCode, Intent data)处理返回的结果,数据在data里。
package com.example.fragment;
}
2.activity 类实现 主要实现是红色部分
使用getIntent()接收fragment 发送的结果。
使用setResult(RESULT_CANCELED, this.getIntent().putExtras(bundle)); activity返回时返回结果该fragment。
package com.example.optimaltangbao;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class NursePrescriptionEditActivity extends Activity {
private final static int date_dialog_from = 0;
private final static int date_dialog_to= 1;
private Calendar calendar = null;
EditText editTextdrug;
EditText editTextnote;
TextView textViewform;
TextView textViewto;
RelativeLayout RelativeLayoutfrom;
RelativeLayout RelativeLayoutto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nurse_prescription_edit);
init_view_id();
}
void init_view_id()
{
editTextdrug=(EditText)findViewById(R.id.editText1);
editTextnote=(EditText)findViewById(R.id.editText2);
textViewform=(TextView)findViewById(R.id.textView3);
textViewto=(TextView)findViewById(R.id.textView5);
RelativeLayoutfrom=(RelativeLayout)findViewById(R.id.relativeLayout11);
RelativeLayoutto=(RelativeLayout)findViewById(R.id.relativeLayout12);
Intent intent = getIntent();
editTextdrug.setText(intent.getStringExtra("drug"));
editTextnote.setText(intent.getStringExtra("note"));
textViewform.setText(intent.getStringExtra("datefrom"));
textViewto.setText(intent.getStringExtra("dateto"));
RelativeLayoutfrom.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v)
{
showDialog(date_dialog_from);
}
});
RelativeLayoutto.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v)
{
showDialog(date_dialog_to);
}
});
}
@Override
protected Dialog onCreateDialog(final int id) {
Dialog dialog = null;
calendar = Calendar.getInstance();
dialog = new DatePickerDialog(
this,
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker dp, int year,int month, int dayOfMonth) {
switch(id)
{
case 0:
textViewform.setText(year + "-" + (month+1) + "-" + dayOfMonth);
break;
case 1:
textViewto.setText(year + "-" + (month+1) + "-" + dayOfMonth);
break;
}
}
},
calendar.get(Calendar.YEAR), // 传入年份
calendar.get(Calendar.MONTH), // 传入月份
calendar.get(Calendar.DAY_OF_MONTH) // 传入天数
);
return dialog;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Bundle bundle = new Bundle();
bundle.putString("datefrom", textViewform.getText().toString());
bundle.putString("dateto", textViewto.getText().toString());
bundle.putString("drug", editTextdrug.getText().toString());
bundle.putString("note", editTextnote.getText().toString());
setResult(RESULT_CANCELED, this.getIntent().putExtras(bundle));
this.finish();
return true;
}else {
return super.onKeyDown(keyCode, event);
}
}
}