简体   繁体   English

如何将一个活动的edittext字符串传递给另一个活动中的方法?

[英]How to pass an edittext string from an activity to a method in another activity?

I have 2 EditTexts and I want to copy the String of these EditTexts and use them in a method in another activity. 我有2个EditTexts,我想复制这些EditTexts的String并在另一个活动的方法中使用它们。

I was making an app which types some details in two EditTexts and have a button which copies the String of these two EditTexts and paste or use it in a RecyclerView in another Activity. 我正在制作一个应用程序,该应用程序在两个EditText中键入一些详细信息,并具有一个按钮,用于复制这两个EditText的String并将其粘贴或使用在另一个Activity的RecyclerView中。

I tried the Intent and Bundle medthods but could not solve it and actually it was hard to arrange the structure of codes. 我尝试了Intent和Bundle方法,但无法解决,实际上很难安排代码的结构。

This is the activity I want to pass from: 这是我想通过的活动:

btn_save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (etTitle.length() != 0 || etDes.length() != 0){
                addData();
            }else {
                Toast.makeText(DataInput.this, "Please Add Data !", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

private void addData() {
    String titled = etTitle.getText().toString();
    String desed = etDes.getText().toString();
    Intent inte = new Intent();
    Bundle bund = new Bundle();
    bund.putString("title", titled);
    bund.putString("des", desed);
    inte.putExtras(bund);
    startActivity(inte);
}

This is the activity I want to receive with: 这是我想接受的活动:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getApplicationContext(), DataInput.class);
            startActivity(intent);
        }
    });

    recyclerView = findViewById(R.id.rv);

    dAdapter = new DataAdapter(dataList);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(dAdapter);
}

public void sendData() {
    Bundle bundle = getIntent().getExtras();
    String addedTitle = bundle.getString("title");
    String addedDes = bundle.getString("dec");
    Data data = new Data(addedTitle, addedDes);
    dataList.add(data);
    dAdapter.notifyDataSetChanged();
}

All I want is to pass the intent and bundle from addData method in the first Activity to the sendData method in the second Activity, so I can use the Strings to pass them in Data constructor. 我想要的只是将意图和包从第一个Activity中的addData方法传递到第二个Activity中的sendData方法,因此我可以使用Strings在Data构造函数中传递它们。

要从EditText检索文本,可以使用editText.getText().toString()

Use bundle or intent. 使用捆绑或意图。

// From activity1 to activity2
    Intent intent = new Intent(Activity1.this, Activity2.class);
    Bundle bundle = new Bundle();
    bundle.putString(<key>, <value>);
    intent.putExtras(bundle);
    startActivity(intent);

    // in activity 2 onCreate
    Bundle bundle = intent.getExtras();
    if (bundle != null) {
        // get the value from bundle based on key
    }

Here is the short example pass data from activity to activity 这是在活动之间传递数据的简短示例

I would recommend you to change your implementation in following way so that we can avoid key mismatch issue accidently. 我建议您按照以下方式更改您的实现,以免我们意外避免密钥不匹配问题。

    @Override
    public void onClick(View v) {
        if (etTitle.length() != 0 || etDes.length() != 0){
            String title = etTitle.getText().toString();
            String description = etDes.getText().toString();
            Activity2.launch(this,title,description);
        } else {
            Toast.makeText(DataInput.this, "Please Add Data !", Toast.LENGTH_SHORT).show();
        }
    }

In calling activity you may create helper method say launch like below. 在调用活动中,您可以创建帮助器方法,例如如下所述启动。

    public static final String KEY_TITLE = "title";
    public static final String KEY_DESCRIPTION = "description";

    public static void launch(Context context, String title, String description) {
        Intent intent = new Intent(context, Activity2.class);
        Bundle data = new Bundle();
        data.putString(KEY_TITLE, title);
        data.putString(KEY_DESCRIPTION, description);
        intent.putExtras(data);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            String title = bundle.getString(KEY_TITLE);
            String description = bundle.getString(KEY_DESCRIPTION);
        }
    }

To retrieve the text from an EditText use 要从EditText检索文本,请使用

String value = editText.getText().toString();

And then pass the key value pair either through intent or bundle 然后通过意图或捆绑传递键值对

Intent in = new Intent(Activity1.this, Activity2.class);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.putExtra("key", value);
startActivity(in);

to receive put this in new activity 接受把它放在新活动中

String string_name = getIntent().getExtras().getString("key");

Update: There is key mismatch, you are sending key as "des" and receiving as "dec" 更新:密钥不匹配,您以“ des”发送密钥,以“ dec”接收

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

相关问题 将字符串从 onLoadFinished 方法传递到另一个活动 - Pass string from onLoadFinished method to another activity 将文本从EditText转换为字符串以进行其他活动 - Get text from EditText into string to another activity 将方法传递给另一个活动 - Pass a method to another activity 如何在另一个活动的 Textview 中传递用户 EditText 输入 - How to pass users EditText input in a Textview in another activity 如何将多个edittext值传递到Arraylist中,然后将其发送到另一个活动? - How to pass multiple edittext values into a Arraylist and then send it to another activity? 如何将信息从一个活动的EditText传递到另一个活动,以将URL加载到该新活动的WebView中? - How do I pass info from one activity's EditText to another for the url to be loaded in that new activity's WebView? 如何将字符串Arraylist从一个活动传递到另一个活动? - How to pass a string Arraylist from one activity to another? 将字符串从一个 Activity 传递到 AndroidStudio 上的另一个 Activity - Pass a String from one Activity to another Activity on AndroidStudio 将字符串从一个活动传递到另一个活动,Android - Pass String from one Activity to another activity, android 如何通过从userinput(edittext)获取文本并将其放入另一个活动的textview中? - How to pass get text from userinput (edittext) and put into a textview in another activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM