0

サブアクティビティから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;
        }
    }
4

1 に答える 1

1

まず第一に、ユーザーが「戻る」ボタンを押した場合、いかなる種類のアクションも試みるべきではありません。これは「今すぐここから出て行け」という意味のグローバルボタンであり、通常、ユーザーはその結果として1つの画面に戻る以外に何も望んでいないと理解されています。

したがって、実行する必要があるのは、searchButton.setOnClickListenerのonClickで、次のように空のインテントを作成することです。

Intent data = new Intent();

次に、次のように、EditTextの値を追加の値として追加する必要があります。

data.putExtra(SBooksDbAdapter.KEY_TITLE_RAW, mTextSearch.getText().toString());

最後に、このインテントをsetResult呼び出しに含めます。

setResult(RESULT_OK, data);

onActivityResultで、すでに行っているようにインテントから値を引き出します。これで問題ありません。

于 2009-08-11T01:05:01.337 に答える