简体   繁体   English

将文本从EditText转换为字符串以进行其他活动

[英]Get text from EditText into string to another activity

I've been searching and I don't find the way. 我一直在搜索,但找不到路。 The solutions I found doesn't work or I don't know how to implement them. 我发现的解决方案不起作用,或者我不知道如何实现它们。

My Main Activity 我的主要活动

public class MainActivity extends AppCompatActivity {



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

        EditText txtDescription = (EditText) findViewById(R.id.nameE);
        final String nameE2 = txtDescription.getText().toString();

        Button bVerify = (Button) findViewById(R.id.verifyB);
        bVerify.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                /** Called when the user clicks the Next button */
                Log.d("EditText", nameE2);
                Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                intent.putExtra(EXTRA_MESSAGE, nameE2);
                startResultActivity(null);
            }
        });
    }

    public void startResultActivity(View view){
        Intent sra = new Intent(this, ResultActivity.class);
        startActivity(sra);

    }
    }

This, already in my code, are the 2 solutions I found everywere, but I don't know how to use them: 这是我的代码中已经找到的两个解决方案,但是我不知道如何使用它们:

 EditText txtDescription = (EditText) findViewById(R.id.nameE);
    String nameE2 = txtDescription.getText().toString();

&

Log.d("EditText", nameE2);
        Intent intent = new Intent(MainActivity.this, ResultActivity.class);
        intent.putExtra(EXTRA_MESSAGE, nameE2);

Inside activity_main, among other things, I've this EditText and this button: 在activity_main中,除其他外,我有这个EditText和这个按钮:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="Name"
    android:ems="10"
    android:id="@+id/nameE"
    android:layout_below="@+id/NameTxt"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:editable="false" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/verifyB"
    android:layout_alignParentStart="true"
    android:text="@string/verifyB"
    android:layout_alignParentEnd="true"
    android:layout_below="@+id/nameE"
    android:layout_alignParentLeft="true" />

In the "ractivity" layout I've this PlainText. 在“ ractivity”布局中,我有这个PlainText。 In this one I want to add the EditText input from the other activity/layout. 在这一部分中,我想从其他活动/布局中添加EditText输入。

<TextView

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello  + nameE2 + . Are you sure you want to accept these terms?"
    android:id="@+id/textView"
    android:textSize="20dp"
    android:textColor="@color/abc_input_method_navigation_guard" />

"nameE2" must be the string of the EditText. “ nameE2”必须是EditText的字符串。

I would love to know how to solve it. 我很想知道如何解决。

Thanks in advance. 提前致谢。 If you need additional data, tell me. 如果您需要其他数据,请告诉我。

Use 采用

 startActivity(intent); 

instead of 代替

 startResultActivity(null);

in your Button onClick 在您的Button onClick

This seems fine, with a little modification. 稍作修改,这似乎很好。

bVerify.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        /** Called when the user clicks the Next button */
        Log.d("EditText", nameE2);
        Intent intent = new Intent(MainActivity.this, ResultActivity.class);
        intent.putExtra(EXTRA_MESSAGE, nameE2);
        startResultActivity(null);
    }
});

Here instead of pass null to startResultActivity() , pass the created Intent similar to this: 在这里,不是将null传递给startResultActivity() ,而是传递类似于以下内容的创建的Intent:

bVerify.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        final String nameE2 = txtDescription.getText().toString();
        /** Called when the user clicks the Next button */
        Log.d("EditText", nameE2);
        Intent intent = new Intent(MainActivity.this, ResultActivity.class);
        intent.putExtra(EXTRA_MESSAGE, nameE2);
        startResultActivity(intent);
    }
});

And the startResultActivity() method have to look like this: 并且startResultActivity()方法必须看起来像这样:

public void startResultActivity(Intent intent){

    startActivity(intent);

}

EDIT: As I mentioned, you cannot use your variable directly in your layout file. 编辑:正如我提到的,您不能直接在布局文件中使用变量。

You have to set it dynamically. 您必须动态设置。 You can do that in your ResultActivity's onCreate() method. 您可以在ResultActivity的onCreate()方法中执行此操作。 It have to look similar to this: 它必须看起来类似于:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ractivity); // your second layout file

    TextView textView = (TextView) findViewById(R.id.textView); 
    String stringResult = getIntent().getStringExtra(EXTRA_MESSAGE);
    String textViewText = "Hello "+ stringResult +". Are you sure you want to accept these terms?";
    textView.setText(textViewText);

}

This will work if in your second layout file there is a Textview with textView id. 如果在你的第二个布局文件是有这将工作TextviewTextView的 ID。

Try this code : 试试这个代码:

 public class MainActivity extends AppCompatActivity {

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

                EditText txtDescription = (EditText) findViewById(R.id.nameE);
                final String nameE2 = txtDescription.getText().toString();

                Button bVerify = (Button) findViewById(R.id.verifyB);
                bVerify.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        /** Called when the user clicks the Next button */
                        Log.d("EditText", nameE2);
                        Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                        intent.putExtra(EXTRA_MESSAGE, nameE2);
                        startActivity(intent);
                    }
                });
            }


            }

changed onClickListener to 将onClickListener更改为

bVerify.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            /** Called when the user clicks the Next button */
            final String nameE2 = txtDescription.getText().toString();
            Log.d("EditText", nameE2);
            Intent intent = new Intent(MainActivity.this, ResultActivity.class);
            intent.putExtra(EXTRA_MESSAGE, nameE2);
            startActivity(intent);
        }
});

you are getting text at start when it does not have any text so move 您开始时没有任何文字时会收到文字,所以请移动

final String nameE2 = txtDescription.getText().toString();

inside onClick of bVerify view so it will read current text when client will click on button 在bVerify视图的onClick内部,因此当客户端单击按钮时它将读取当前文本

Change startResultActivity(null); 更改startResultActivity(null); into startActivity(intent); 进入startActivity(intent); . And then in the other Activity where you need to use that String: 然后在另一个需要使用该字符串的Activity中:

Intent intent = getIntent();
String getExtra = intent.getStringExtra("EXTRA_MESSAGE");
TextView.setText(getExtra);

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

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