【问题标题】:Get EditText's value from sub Activity从子 Activity 中获取 EditText 的值
【发布时间】:2010-11-18 07:07:31
【问题描述】:

如何从子Activity中获取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;
        }
    }

【问题讨论】:

  • 你的意思是你想返回一个字符串给调用活动? startActivityForResult(...) & setResult(resultCode, data) 然后。
  • 我想从Sub-Activity的TextView中获取String值。但是当我打开子活动,然后我点击手机上的后退按钮时,它会抛出异常错误。问题是当我尝试解决异常错误(管理活动生命周期)时,不再有异常,但我无法获取字符串值!那么如何获取String并无一例外地点击手机上的返回按钮呢?
  • 您能否发布一些代码 sn-ps,以便更容易准确地理解您正在尝试什么以及为什么 startActivityForResult 调用不能满足您的需要?
  • 哦,真的很抱歉,我这里有点混乱。我的意思是 EditText 不是 TextView。

标签: android android-activity android-edittext


【解决方案1】:

首先,如果用户点击“返回”按钮,您不应该尝试任何类型的操作。这是一个全局按钮,意思是“现在让我离开这里”,通常理解为用户除了返回一个屏幕之外别无所求。

所以你需要做的是在你的searchButton.setOnClickListener中,在onClick中,像这样创建一个空Intent:

Intent data = new Intent();

然后您需要将 EditText 的值添加为额外值,如下所示:

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

最后,在你的 setResult 调用中包含这个意图:

setResult(RESULT_OK, data);

在你的 onActivityResult 中,像你已经在做的那样从意图中提取价值,你应该没问题。

【讨论】:

  • 感谢 MattC ,但我尝试使用您的代码,但我不知道为什么单击“搜索”按钮后我的 ListView 上没有任何内容?我认为没有数据回调,您对此有什么想法吗?
  • 如果调试代码并设置断点,extras.getString(SBooksDbAdapter.KEY_TITLE_RAW) 返回的值是EditText框中的值吗?
  • 调试后返回值为“null”。不知道为什么?
  • 有一点,当我更改代码时 Bundle extras = getIntent().getExtras(); With Bundle extras = intent.getExtras();结果是好的,但是当点击手机上的返回“按钮”时,它会抛出一个错误。反之,值为空。
  • 调试的时候mTextSearch.getText()返回什么?我在我的一些应用程序的很多地方都这样做了,它就像一个魅力,所以我不太确定你的情况如何:(
猜你喜欢
  • 1970-01-01
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多