我试图制作一个按钮,当我点击它时移动到另一个活动,填写2个字段" name"和"家庭"单击“保存”并返回并显示TextView" fullname"
firstActivity:
public class ToDo extends AppCompatActivity {
private static final int Request_Code=1001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do);
Button AddNote=findViewById(R.id.todo_btnAddNote);
AddNote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(ToDo.this, Todo_saveActivity.class);
startActivity(intent);
startActivityForResult(intent,Request_Code);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==Request_Code &&
resultCode==RESULT_OK &&
data !=null){
String fullname=data.getStringExtra(Todo_saveActivity.result_note);
Toast.makeText(ToDo.this,fullname,Toast.LENGTH_LONG).show();
}
}
}
第二次活动:
public class Todo_saveActivity extends AppCompatActivity {
public static final String result_note="result";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todo_save);
final EditText fullname=findViewById(R.id.todo_name);
final EditText savefamily=findViewById(R.id.todo_family);
Button savenote=findViewById(R.id.btn_savenote);
savenote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.putExtra(result_note,fullname+savefamily.getText().toString());
setResult(RESULT_OK);
finish();
}
});
}
}
当我点击" AddNote"打开2个活动(IDK为什么),填充后返回firstActivity和Toast不显示....
我初学者不知道该怎么做,请在填写字段后给我写一个代码在firstActivity上显示为textview 感谢。
答案 0 :(得分:0)
在点击监听器中,您需要删除此行startActivity(intent);
当你想使用onActivityResult时,你可以像你一样调用startActivityForResult(intent,Request_Code);
,但由于你之前有startActivity(intent);
,我认为它开始了第二个活动而不知道你想要使用onActivityResult。