Android应用开发视频教程-第1季

Android应用开发视频教程-第1季

5 (35人评价)
  • 课时:(50)

  • 学员:(743)

  • 浏览:(20912)

  • 加入课程

1.6_Activity回传值的笔记

相关课时: 笔记详情:

//MainActivity

public class MainActivity extends Activity {

private EditText edit;
private Button button;
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

edit = (EditText) this.findViewById (R.id.eidt);
button = (Button) this.findViewById(R.id.but);
text = (TextView) this.findViewById(R.id.text);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String s =edit.getText().toString().trim();
Intent intent = new Intent(MainActivity.this,FindEdit.class);
intent.putExtra("edit", s);
startActivityForResult(intent, 840626);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 840626 && resultCode == 617724){
String a = data.getStringExtra("edit1").toString().trim();
text.setText(a);
}
}
}

//FindEdit

public class FindEdit extends Activity{
private EditText edit ;
private TextView text;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.findedit_activity);

text = (TextView) findViewById (R.id.text);
edit = (EditText) findViewById (R.id.edit1);
button = (Button) findViewById (R.id.button);


Intent intent = getIntent();
String s = intent.getStringExtra("edit").toString().trim();
text.setText(s);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String str = edit.getText().toString().trim();
Intent ent = new Intent();
ent.putExtra("edit1", str);

setResult(617724, ent);
finish();
}
});


}

}

 

0 0

你感兴趣的课程

8万+浏览/ 916学员/ 4.5评分
免费
6万+浏览/ 177学员/ 5评分
免费
6万+浏览/ 972学员/ 4.8评分
免费