更新显示用户输入文本的Textview,Intent.getStringExtra()

时间:2014-02-08 16:03:54

标签: android-intent

我的变量声明如下:

 static private final int GET_TEXT_REQUEST_CODE = 1;
        static private final String URL = "http://www.google.com";
        static private final String TAG = "Lab-Intents";


        static private final String CHOOSER_TEXT = "Load " + URL + " with:";


        private TextView mUserTextView;

我不确定如何在onActivityResult()方法中完成代码。我不确定是否更新了正确显示用户输入文本的Textview。我必须使用Intent.getStringExtra()。我使用但作为getIntent()。getStringExtra(“mUserTextView”);

private void startImplicitActivation() {

        Log.i(TAG, "Entered startImplicitActivation()");



                 Uri url = Uri.parse(URL);            
                 Intent baseIntent = new Intent(Intent.ACTION_VIEW, url);
             Intent chooserIntent = Intent.createChooser(baseIntent, CHOOSER_TEXT);
             Log.i(TAG,"Chooser Intent Action:" + chooserIntent.getAction());

             startActivity(chooserIntent);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    Log.i(TAG, "Entered onActivityResult()");

    // TODO - Process the result only if this method received both a
    // RESULT_OK result code and a recognized request code
    // If so, update the Textview showing the user-entered text.

if (requestCode == GET_TEXT_REQUEST_CODE){
    if (requestCode == RESULT_OK){
    getIntent().getStringExtra("mUserTextView");    
    }
}

}
}

3 个答案:

答案 0 :(得分:2)

要更新标签,您应该使用:

if ( resultCode == RESULT_OK && requestCode == GET_TEXT_REQUEST_CODE ) {
    mUserTextView.setText( data.getStringExtra("resultado"));
}

请记住使用请求代码GET_TEXT_REQUEST_CODE调用“ExplicitlyLoadedActivity”:

Intent myIntent = new Intent(ActivityLoaderActivity.this, ExplicitlyLoadedActivity.class); 
startActivityForResult( myIntent, GET_TEXT_REQUEST_CODE ); // just to be sure about Request code

更改单词“resultado”,用于在enterlicklyLoadedActivity的enterClicked()中使用的单词

String givenText = mEditText.getText().toString(); // Getting the user input
Intent intencion = new Intent(); // Getting ready to comeback
intencion.putExtra("resultado", givenText); // I use "resultado", spanish guy
setResult(RESULT_OK, intencion); // RESULT_OK is a constant = 1 
finish();

祝你好运在实验室中扩展实验室,我也是这样做的。干杯

答案 1 :(得分:0)

应该以静态方式访问Intent类型的静态方法createChooser(Intent, CharSequence)。当我在chooserIntent.createChooser(map,CHOOSER_TEXT);

运行相同的代码时会显示此信息

// CODE

private void startImplicitActivation() 
{
    Log.i(TAG, "Entered startImplicitActivation()");

    // TODO - Create a base intent for viewing a URL 
    // (HINT:  second parameter uses parse() from the Uri class)
    Intent map = new Intent(Intent.ACTION_VIEW,Uri.parse(URL));


    // TODO - Create a chooser intent, for choosing which Activity
    // will carry out the baseIntent. Store the Intent in the 
    // chooserIntent variable below. HINT: using the Intent class' 
    // createChooser())


    Intent chooserIntent=null;
    chooserIntent.createChooser(map,CHOOSER_TEXT);

    Log.i(TAG,"Chooser Intent Action:" + chooserIntent.getAction());
    // TODO - Start the chooser Activity, using the chooser intent
    startActivity(chooserIntent);

}

当我运行应用程序时,这会导致错误:

unfortunately the project has stopped

答案 2 :(得分:0)

chooserIntent为null。首先创建它: Intent chooserIntent = Intent.createChooser(map,CHOOSER_TEXT);