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



我的变量声明如下:

 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()方法中完成代码。我不确定我是否正确更新了显示用户输入的文本的文本视图。我必须使用 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");    
    }
}
}
}

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

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

请记住调用"ExplicitlyLoadedActivity",请求代码GET_TEXT_REQUEST_CODE:

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

更改单词"resultado",用于您在 enterClicked() 中用于 ExplicitlyLoadedActivity 的

单词
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();

祝你好运,实验室在课程中,我也在做。干杯

应以静态方式访问从 Intent 类型createChooser(Intent, CharSequence)的静态方法。当我在chooserIntent.createChooser(map,CHOOSER_TEXT);运行相同的代码时,会显示这一点

//法典

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

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

最新更新