获得从子活动的EditText的价值
-
12-09-2019 - |
题
如何从子活动的EditText的价值?随着一个条件,如果我点击后退按钮的手机上,有一个在子活动没有错误?
这是我的子活动强>代码:
public class SBooksSearch extends Activity {
private EditText mTextSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sbooks_search);
mTextSearch = (EditText)findViewById(R.id.edit_search);
Button searchButton = (Button)findViewById(R.id.btn_search);
searchButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent data = new Intent();
data.putExtra(SBooksDbAdapter.KEY_TITLE_RAW, mTextSearch.getText().toString());
setResult(RESULT_OK, data);
finish();
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
}
@Override
protected void onPause(){
super.onPause();
}
@Override
protected void onResume(){
super.onResume();
}
}
这是我的活动-结果:强>
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
switch(requestCode){
case ACTIVITY_SEARCH:
Bundle extras = getIntent().getExtras();
mTitleRaw = extras != null ? extras.getString(SBooksDbAdapter.KEY_TITLE_RAW) : null;
if(mTitleRaw!=null){
Cursor cursor = mDbHelper.searchData(mTitleRaw);
String[] from = new String[]{ SBooksDbAdapter.KEY_ROWID,
SBooksDbAdapter.KEY_TITLE, SBooksDbAdapter.KEY_LYRICS };
int[] to = new int[]{ R.id.id, R.id.title, R.id.lyrics };
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, R.layout.sbooks_row, cursor, from, to );
setListAdapter(adapter);
}
break;
}
}
解决方案
首先,你不应该,如果用户点击“后退”按钮尝试任何形式的行动。这是一个全球性的按钮,意思是“让我离开这儿”,并且它通常被理解用户想要什么比回到一个屏幕出现作为一个结果等。
所以,你需要做的是在你的searchButton.setOnClickListener,在onclick,创建一个空的意图,像这样:
Intent data = new Intent();
然后,你需要将你添加的的EditText的值作为额外的价值,像这样:
data.putExtra(SBooksDbAdapter.KEY_TITLE_RAW, mTextSearch.getText().toString());
最后,包括此意图在您的setResult电话:
setResult(RESULT_OK, data);
在您的onActivityResult拉值移出意图就像你已经在做,你应该罚款。
不隶属于 StackOverflow