简体   繁体   English

在另一个Activity中显示动态创建的EditText的值

[英]Display value of dynamically created EditText in another Activity


I created one mini App for learning, 我创建了一个用于学习的迷你应用,
When any user give any number in EditText and click on submit, then TextView & EditText created dynamically based on given Digit. 当任何用户在EditText中输入任意数字并单击Submit时,TextView&EditText将基于给定的Digit动态创建。
and After created new EditText, user fill Data in EditText and click on submit button... 创建新的EditText之后,用户在EditText中填充数据,然后单击Submit按钮...
When user click on submit data must pass in new intent and Display that data in it. 当用户单击提交数据时,必须传递新的意图并在其中显示该数据。

My Problem is that when I click on submit button, 我的问题是,当我单击“提交”按钮时,
only Last EditText Value is display... 仅显示最后一个EditText值...

What Can I Do? 我能做什么?
Thanks In Advance...!!! 提前致谢...!!!
Here is my Code... 这是我的代码...

activity_main.xml activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="Enter Digit Here" >

            <requestFocus />
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button1"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ADD" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent" >

                <Button
                    android:id="@+id/button2"
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="DELETE" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent" >

                <Button
                    android:id="@+id/button3"
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="SUBMIT" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >


    </LinearLayout>

</LinearLayout>


MainAvtivity.java MainAvtivity.java

package com.example.textviewdemo;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    private TextView addTv;
    private EditText edt, edtAdd, edArray;
    private Button add, delete, submit;
    LinearLayout layout;
    List<EditText> allEds;

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

        edt = (EditText) findViewById(R.id.editText1);
        add = (Button) findViewById(R.id.button1);
        delete = (Button) findViewById(R.id.button2);
        submit = (Button) findViewById(R.id.button3);

        layout = (LinearLayout) findViewById(R.id.LinearLayout);

        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int no = Integer.parseInt(edt.getText().toString());
                allEds = new ArrayList<EditText>();

                for (int i = 0; i < no; i++) {

                    addTv = new TextView(MainActivity.this);

                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT);
                    layout.setLayoutParams(params);
                    addTv.setText("TextView " + i);
                    addTv.setId(i);
                    layout.addView(addTv);

                    edtAdd = new EditText(MainActivity.this);

                    layout.setLayoutParams(params);
                    allEds.add(edtAdd);
                    edtAdd.setText("Test" + i);
                    edtAdd.setId(i);
                    layout.addView(edtAdd);
                }
            }
        });
        submit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent data = new Intent(MainActivity.this, Display.class);
                String[] items = new String[allEds.size()];
                String str = String.valueOf(allEds.size());
                for (int j = 0; j < allEds.size(); j++) {
                    items[j] = allEds.get(j).getText().toString();
                    data.putExtra("edData", items[j]);
                    data.putExtra("size", str);
                    /*
                     * Toast.makeText(getApplicationContext(), items[j],
                     * Toast.LENGTH_SHORT).show();
                     */
                }

                startActivity(data);
            }
        });
        delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                layout.removeAllViews();

            }
        });
    }
}

Display.xml Display.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/LinearLayout_1" >
    </LinearLayout>

    </LinearLayout>

Display.java Display.java

package com.example.textviewdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Display extends ActionBarActivity {

    TextView getText;
    LinearLayout linear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display);
        Intent get = getIntent();

        linear = (LinearLayout) findViewById(R.id.LinearLayout_1);

        int size = Integer.parseInt((get.getExtras().getString("size")));
        for (int i = 0; i < size; i++) {
            getText = new TextView(Display.this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            getText.setText(get.getExtras().getString("edData"));
            linear.setLayoutParams(params);
            getText.setId(i);
            linear.addView(getText);
        }

    }
}
My Problem is that when I click on submit button,
only Last EditText Value is display...

Because, you are overwriting the data in your for loop using this key: 因为,您正在使用此键覆盖for循环中的数据:

for (int j = 0; j < allEds.size(); j++) {
    items[j] = allEds.get(j).getText().toString();
    data.putExtra("edData", items[j]);
    data.putExtra("size", str);
}

And later, retrieve using index id till j = size. 然后,使用索引id检索直到j = size。

You can either use index in key or pass String array itself: 您可以在键中使用index或传递String数组本身:

Approach 1: 方法1:

data.putExtra("size", str);  //you don't need to keep this in loop as its same.
for (int j = 0; j < allEds.size(); j++) {
    items[j] = allEds.get(j).getText().toString();
    data.putExtra("edData"+j, items[j]);
}

and

int size = Integer.parseInt(getIntent().getStringExtra("size"));
for (int j = 0; j < size; j++) {
    Intent intent = getIntent();
    String str = intent.getStringExtra("edData"+j);
}

Approach 2: 方法二:

Pass String array in intent, and later, retrieve it in second activity: 传递意图的String数组,然后在第二个活动中检索它:

intent.putExtra("strings", myStringArray);

and

Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("strings");

Hope this helps. 希望这可以帮助。

Please check below you are updating the value every time that's why it is only showing last digit. 请检查以下内容,您每次都在更新值,这就是为什么它仅显示最后一位数字。

package com.example.textviewdemo;

    import java.util.ArrayList;
    import java.util.List;

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.View;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.TextView;

    public class MainActivity extends ActionBarActivity {

        private TextView addTv;
        private EditText edt, edtAdd, edArray;
        private Button add, delete, submit;
        LinearLayout layout;
        List<EditText> allEds;

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

            edt = (EditText) findViewById(R.id.editText1);
            add = (Button) findViewById(R.id.button1);
            delete = (Button) findViewById(R.id.button2);
            submit = (Button) findViewById(R.id.button3);

            layout = (LinearLayout) findViewById(R.id.LinearLayout);

            add.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    int no = Integer.parseInt(edt.getText().toString());
                    allEds = new ArrayList<EditText>();

                    for (int i = 0; i < no; i++) {

                        addTv = new TextView(MainActivity.this);

                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                LayoutParams.WRAP_CONTENT,
                                LayoutParams.WRAP_CONTENT);
                        layout.setLayoutParams(params);
                        addTv.setText("TextView " + i);
                        addTv.setId(i);
                        layout.addView(addTv);

                        edtAdd = new EditText(MainActivity.this);

                        layout.setLayoutParams(params);
                        allEds.add(edtAdd);
                        edtAdd.setText("Test" + i);
                        edtAdd.setId(i);
                        layout.addView(edtAdd);
                    }
                }
            });
            submit.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent data = new Intent(MainActivity.this, Display.class);
                    String[] items = new String[allEds.size()];
                    String str = String.valueOf(allEds.size());
                    for (int j = 0; j < allEds.size(); j++) {
                        items[j] = allEds.get(j).getText().toString();
                        /*
                         * Toast.makeText(getApplicationContext(), items[j],
                         * Toast.LENGTH_SHORT).show();
                         */
                    }
                    //changes made here
                        data.putExtra("edData", items);
                        data.putExtra("size", str);

                    startActivity(data);
                }
            });
            delete.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    layout.removeAllViews();

                }
            });
        }
    }



package com.example.textviewdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Display extends ActionBarActivity {

    TextView getText;
    LinearLayout linear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display);
        Intent get = getIntent();

        linear = (LinearLayout) findViewById(R.id.LinearLayout_1);

        int size = Integer.parseInt((get.getExtras().getString("size")));
        for (int i = 0; i < size; i++) {
            getText = new TextView(Display.this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);


            String[] data = get.getExtras().getString("edData");
            String textOnRequest = ";
            for(int i=0 ; i < data.length ; i++){
                textOnResponse += data[i];
            }
            getText.setText(textOnResponse);
            linear.setLayoutParams(params);
            getText.setId(i);
            linear.addView(getText);
        }

    }
}

You can use Shared Preferences to store values, And later on you can retrieve from shared preferences. 您可以使用“共享首选项”来存储值,然后可以从“共享首选项”中检索。

Write to Shared Preferences 写入共享首选项

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();

Read from Shared Preferences 从共享首选项中读取

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

Check in Google API Shared - Preferences 签入Google API 共享-首选项

Move data.putExtra for both size and edData keys outside for loop data.putExtra work like a Map which have a unique keys sizeedData键的data.putExtra都移动for循环data.putExtra外部data.putExtra工作方式类似于具有唯一键的Map

// Prepare items Array 
for (int j = 0; j < allEds.size(); j++) {
    items[j] = allEds.get(j).getText().toString();
}
data.putExtra("edData", items);
data.putExtra("size", str);

Now get edData as String Array instead of String from Bundle . 现在将edData作为String Array而不是Bundle的String。

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

相关问题 选中单选按钮时如何在另一个活动中显示EditText输入? - How to display EditText input in another activity when Radio Button is checked? 如何将 EditText 值从一个活动到另一个活动作为 int 获取到 go - How to get an EditText value to go from one activity to another as an int 与片段中动态创建的EditText交互 - Interacting with a dynamically created EditText in a fragment 将各种editText传递给另一个活动 - Passing various editText to another activity 在按下返回按钮后再次创建活动时,EditText返回相同的值 - EditText returning same value when activity is created again after the back button is pressed 如何使用SharedPreferences保存在edittext中输入的文本,并将其显示在另一个Activity中的TextView中 - How to use SharedPreferences to save the text entered in edittext and display it in TextView in another Activity 从每个动态创建的EditText获取数据 - Get data from each dynamically created EditText 从动态创建的 EditText 中获取文本 - Get text from dynamically created EditText 检查动态创建的类是否是一个活动 - Check if dynamically created class is an activity 我正在尝试使用另一个活动中的 EditText 的值更改 TextView 的文本 - I'm trying to change the text of a TextView with the value of one that is EditText in another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM