Android 编程帮助

发布于 2024-11-08 18:20:30 字数 125 浏览 0 评论 0原文

我正在开发一个应用程序,其中一个布局中有文本视图,还有一个按钮可以将您发送到带有 Edittext 的第二个布局。每个编辑文本都是一个文本视图。如何使用第二个布局中的按钮将 textview 中的文本替换为 edittext 中的文本?

I'm working on an app where i have textview's in one layout and a button that sends you to a second layout with Edittext's. Every edittext is for an textview. How can i replace text in a textview with the text in edittext with a button in the second layout?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

熟人话多 2024-11-15 18:20:30

你的意思是这样?

在方法 onCreate() 中:

btn.setOnClickListener(this);
txtView = (TextView)findViewById(R.id.mytxtView);
editTxt = (EditText) findViewById(R.id.myeditText);

然后,像这样重写 onClick 方法:

@Override
public void onClick(View v ) {
txtView.setText(editText.getText());
}

you mean like this ??

in the method onCreate() :

btn.setOnClickListener(this);
txtView = (TextView)findViewById(R.id.mytxtView);
editTxt = (EditText) findViewById(R.id.myeditText);

and then , ovverride the onClick method like this :

@Override
public void onClick(View v ) {
txtView.setText(editText.getText());
}
池予 2024-11-15 18:20:30
textview textview = (textview)findViewById(R.layout.nameoftextview);
edittext edittext = (edittext)findViewById(R.layout.nameofedittext);
textview.settext(edittext.text());
textview textview = (textview)findViewById(R.layout.nameoftextview);
edittext edittext = (edittext)findViewById(R.layout.nameofedittext);
textview.settext(edittext.text());
辞取 2024-11-15 18:20:30

首先,您必须通过意图将 edittext 值传递给第一个活动。

例如:

Intent i = new Intent(this, FirstActivity.class);
i.putExtra("edittext_value", edittext.getText().toString());
startActivity(i);

然后在您的第一个活动中,您将必须获取此数据:

String value;
Bundle extras = this.getIntent().getExtras();

if (extras != null) {
     value = extras.getString("edittext_value");
     textview.setText(value);
}

希望这可以帮助您。

First of all, you will have to pass the edittext value to the first activity through intent.

Eg:

Intent i = new Intent(this, FirstActivity.class);
i.putExtra("edittext_value", edittext.getText().toString());
startActivity(i);

Then inside your first activity, you will have to fetch this data as:

String value;
Bundle extras = this.getIntent().getExtras();

if (extras != null) {
     value = extras.getString("edittext_value");
     textview.setText(value);
}

Hope this may help you.

甩你一脸翔 2024-11-15 18:20:30

据我了解,您希望第二个活动(我们称之为 Activity2)将文本传递回第一个活动(Activity1)。为此,您必须(一些代码来自:

更改打开 Activity2 的方式,以将

Intent EditIntent = new Intent(this, Activity2.class);
    //Other stuff you may want to do with intent
startActivityForResult(EditIntent , 0);

覆盖添加到 Activity1

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK && requestCode == 0) {
        if (data.hasExtra("myText")) {
            //get your data with data.getExtras().getString("myText")
        }
    }
}

更改 Activity2 上的按钮的功能

{  
    Intent returnData= new Intent();
    returnData.putExtra("myText", /*Text from your EditText*/);
    if (getParent() == null) { //This part was taken from StackOverflow by Ilya Taranov
        setResult(Activity.RESULT_OK, returnData);
    } else {
        getParent().setResult(Activity.RESULT_OK, returnData);
    }
    finish();
}

这应该将文本从 EditText 从 Activity2 返回到 Activity1。代码未经过测试

From what I understand is that you want your second activity (let's call it Activity2) to pass text back to the first one (Activity1). To do that, you have to (some code comes from :

Change the way you open Activity2 to

Intent EditIntent = new Intent(this, Activity2.class);
    //Other stuff you may want to do with intent
startActivityForResult(EditIntent , 0);

Add override to you Activity1

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK && requestCode == 0) {
        if (data.hasExtra("myText")) {
            //get your data with data.getExtras().getString("myText")
        }
    }
}

Change what button on your Activity2 does

{  
    Intent returnData= new Intent();
    returnData.putExtra("myText", /*Text from your EditText*/);
    if (getParent() == null) { //This part was taken from StackOverflow by Ilya Taranov
        setResult(Activity.RESULT_OK, returnData);
    } else {
        getParent().setResult(Activity.RESULT_OK, returnData);
    }
    finish();
}

This should return text from EditText from Activity2 to Activity1. Code was not tested

喜爱皱眉﹌ 2024-11-15 18:20:30

为文本视图创建一个变量来访问它,就像

Textview txt = (Textview) finviewByid........;

在按钮单击侦听器上实现以下代码一样

txt.setText(edittext.getText().toString());

create a variable for the textview to access it like

Textview txt = (Textview) finviewByid........;

implement the following code on button click listener

txt.setText(edittext.getText().toString());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文