简体   繁体   English

如何将数据传递给我的应用程序中的活动?

[英]How can I pass data to the activity in my app?

If I press the share button that appears by long pressing the text, I want to send the text to other activities of my app.如果我按下通过长按文本出现的共享按钮,我想将文本发送到我的应用程序的其他活动。 I move to another activity, but the text I want is not delivered.我转到另一项活动,但我想要的文本未送达。 What is the problem?问题是什么?

This is the code of the activity that writes the text I want to deliver.这是编写我要传递的文本的活动的代码。

public class MainActivity extends AppCompatActivity {公共类 MainActivity 扩展 AppCompatActivity {

EditText editText;
Intent sendIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText = (EditText) findViewById(R.id.editText);
    sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_VIEW);
    editText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendIntent.putExtra("TEXT", editText.getText().toString());
            sendIntent.setType("text/*");
        }
    });
}

} This is the intent-filter of my xml code.这是我的 xml 代码的意图过滤器。

<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <action android:name="android.intent.action.MAIN"/>

    <category android:name="android.intent.category.LAUNCHER" />
    <data android:mimeType = "text/*"/>
</intent-filter>

This is the part that receives the intent of the activity that I want to receive data from.这是接收我想要从中接收数据的活动意图的部分。

Intent receiveIntent = getIntent();意图 receiveIntent = getIntent(); url = receiveIntent.getStringExtra("TEXT"); url = receiveIntent.getStringExtra("TEXT");

webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl(url); webView.loadUrl(url); This is the intent-filter part of the xml code of the activity where I want to receive data.这是我要接收数据的活动的 xml 代码的意图过滤器部分。

<activity android:name=".MyWebBrowser"
    android:label="MyWebBrowser">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />


    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/*"/>
    </intent-filter>

Here is a simple example for you:这是一个简单的例子:

Instead of long press on editText I simply use a button, but nevertheless, the logic stays almost the same:而不是长按editText我只是使用一个按钮,但尽管如此,逻辑几乎保持不变:

// MainActivity. // 主要活动。 From here we send the text from EditText to SecondActivity.从这里我们将文本从 EditText 发送到 SecondActivity。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.button);
        EditText editText = findViewById(R.id.editText);
        Intent intent = new Intent(this, SecondActivity.class);
        intent.setAction("MyIntentAction");

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.putExtra("TEXT", editText.getText().toString());
                startActivity(intent);
                finish();
            }
        });
    }
}

// SecondActivity. // 第二个活动。 Receives and displays text from MainActivity.从 MainActivity 接收并显示文本。

    public class SecondActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
    
            TextView textView = findViewById(R.id.textView);
    
            Intent intent = getIntent();
            if(intent.getAction().equals("MyIntentAction")) {
                String str = intent.getStringExtra("TEXT");
                textView.setText(str);
            }
    
        }
    }

// For Long Click detection add the following code to MainActivity: // 对于长单击检测,将以下代码添加到 MainActivity:

editText.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                intent.putExtra("TEXT", editText.getText().toString());
                startActivity(intent);
                finish();
                return true;
            }
        });

I see that you are using two activity has LAUNCHER with the MAIN in your manifest.我看到您在清单中使用了两个LAUNCHERMAIN活动。 Please use only one in your manifest .请在您的manifest仅使用一个。

To pass the data to another activity in your app, you can try to use startActivity and put your data in the intent data:要将数据传递给应用中的另一个 Activity,您可以尝试使用startActivity并将数据放入 Intent 数据中:

  • On action to open the MyWebBrowser :打开MyWebBrowser

     val intent = Intent(context, MyWebBrowser::class.java) intent.putExtra("TEXT", yourtext) startActivity(intent)
  • Get the Text in your MyWebBrowser :MyWebBrowser获取文本:

    override fun onCreate(savedInstanceState: Bundle?) {
       ...
       val TEXT = intent.getStringExtra("TEXT")
       textView.text = TEXT
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我的方法不在活动中时,如何将其传递给活动? - How can I pass my method an activity when it is not in an activity? 如何将数据正确地从一项活动传递到另一项活动? - how can I pass data correctly from one activity to another? 如何将数据从活动传递到地理围栏 BroadcastReceiver? - How can I pass data from an Activity to a Geofence BroadcastReceiver? 如何将数据更新为活动? - How can I update my data into my activity? ¿ 如何在后台启动我的应用程序或活动? - ¿How can i launch my app or an activity in background? 如何从Google Map传输数据(路线)并将其传递给我的应用? - How can I transfer data(directions) from Google Map and pass it to my app? 如何将数据从一个活动传递到当前可见/活动活动? 这是一个好的共同点吗? - How can I pass data from one Activity to the current visible/active Activity? Is it a good pattern in common? 如何在我的活动中获取 viewpager 数据 android java - How can I get viewpager data in my activity android java 如何将数据传递到我的片段中以显示我的视图? - How can i pass data into my fragments to my view? 我如何在整个活动中传递实例变量? - how can i pass my instance variable througout my entire activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM