繁体   English   中英

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

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

我有2个EditTexts,我想复制这些EditTexts的String并在另一个活动的方法中使用它们。

我正在制作一个应用程序,该应用程序在两个EditText中键入一些详细信息,并具有一个按钮,用于复制这两个EditText的String并将其粘贴或使用在另一个Activity的RecyclerView中。

我尝试了Intent和Bundle方法,但无法解决,实际上很难安排代码的结构。

这是我想通过的活动:

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);
}

这是我想接受的活动:

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();
}

我想要的只是将意图和包从第一个Activity中的addData方法传递到第二个Activity中的sendData方法,因此我可以使用Strings在Data构造函数中传递它们。

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

使用捆绑或意图。

// 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
    }

这是在活动之间传递数据的简短示例

我建议您按照以下方式更改您的实现,以免我们意外避免密钥不匹配问题。

    @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();
        }
    }

在调用活动中,您可以创建帮助器方法,例如如下所述启动。

    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);
        }
    }

要从EditText检索文本,请使用

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

然后通过意图或捆绑传递键值对

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

接受把它放在新活动中

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

更新:密钥不匹配,您以“ des”发送密钥,以“ dec”接收

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM