【问题标题】:How to get value from **edit text** and display it in a textview in another activity如何从**编辑文本**中获取价值并将其显示在另一个活动的文本视图中
【发布时间】:2017-10-21 15:02:03
【问题描述】:

在过去的 4 个小时里,我一直在拔头发,试图找出如何做到这一点。

我检查了 SO 是否有其他问题,但没有发现任何问题。 我是一名中级程序员,如果您的 cmets 和答案以我能理解的方式编写,我将非常感激。

我希望能够获取用户在InputPage 活动上的@+id/location Edittext 框中输入的文本,并将其显示在InputPage2 活动上的@+id/ListName TextView 中当@ 上的按钮987654326@活动被按下。

InputPage java 代码

public class InputPage extends Activity {
    public String name;
    public InputPage() {
}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_page );
        Button button = (Button)findViewById(R.id.addButton);
    }


    public InputPage(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
    public String name(String name){
        this.name = name;
        EditText editText = (EditText)findViewById(R.id.location);
        String value = editText.getText().toString();
        return value;

    }

    public void toInputScreen2(View view) {
        Button button = (Button) findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent(InputPage.this, InputPage2.class);
                startActivity(intent2);
            }
        });
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".InputPage"
    android:background="@color/BackgroundGrey">

    <TextView
        android:id="@+id/mapNo"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:text="@string/mapNoTextBox"
        android:textColor="@color/textColour"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.04"
        app:layout_constraintVertical_bias="0.13" />

    <TextView
        android:id="@+id/inputPageTitle"
        android:layout_width="178dp"
        android:layout_height="21dp"
        android:text="@string/inputPageTitle"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="@color/textColour"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.01999998" />

    <EditText
        android:id="@+id/noInput"
        android:layout_width="48dp"
        android:layout_height="39dp"
        android:ems="10"
        android:inputType="number"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.13"
        app:layout_constraintHorizontal_bias="0.51" />

    <TextView
        android:layout_width="150dp"
        android:layout_height="38dp"
        android:text="@string/location"
        android:textColor="@color/textColour"
        android:textSize="30sp"
        android:textStyle="bold"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintHorizontal_bias="0.041"
        app:layout_constraintVertical_bias="0.24" />

    <EditText
        android:id="@+id/location"
        android:layout_width="177dp"
        android:layout_height="44dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="@string/hintText"
        android:textColorHint="@color/hintTextColour"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.952"
        app:layout_constraintVertical_bias="0.243" />

    <EditText
        android:id="@+id/dateBox"
        android:layout_width="133dp"
        android:layout_height="44dp"
        android:ems="10"
        android:inputType="date"
        android:hint="@string/dateBoxHint"
        android:textColorHint="@color/hintTextColour"
        android:layout_marginLeft="61dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintHorizontal_bias="0.335"
        app:layout_constraintVertical_bias="0.374" />

    <TextView
        android:layout_width="101dp"
        android:layout_height="34dp"
        android:text="@string/dateBox"
        android:textColor="@color/textColour"
        android:textStyle="bold"
        android:textSize="30sp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.032"
        app:layout_constraintVertical_bias="0.369" />

    <Button
        android:id="@+id/button2"
        android:layout_width="165dp"
        android:layout_height="58dp"
        android:text="@string/continueButton"
        android:background="@drawable/buttons"
        android:layout_marginRight="8dp"
        android:onClick="toInputScreen2"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintVertical_bias="0.778" />


</android.support.constraint.ConstraintLayout>

**InputPage2 java代码**

public class InputPage2 extends Activity  {

    Button saveButton1;
    EditText message;
    String Message;
    TextView[] pairs;
    int num_match;




    public TextView[] getPairs() {
        return pairs;
    }

    public void setPairs(TextView[] pairs) {
        this.pairs = pairs;
    }

    public int getNum_match() {
        return num_match;
    }

    public void setNum_match(int num_match) {
        this.num_match = num_match;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_page2);
        message = (EditText) findViewById(R.id.textNotAtHomes);



    }

    public void toSaveField(View v) {
        saveButton1 = (Button) findViewById(R.id.saveButton1);
        saveButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }

        });


        Message = message.getText().toString();
        try {
            FileOutputStream fos = openFileOutput("Text.txt", MODE_WORLD_READABLE);
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            try {
                osw.write(Message);
                osw.flush();
                osw.close();
                Toast.makeText(getBaseContext(), "Saved successfully", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        Intent intent2 = new Intent(InputPage2.this, MainActivity.class);
        startActivity(intent2);
    }

    public void linearLayout(TextView[] pairs, int num_match) {
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        pairs = new TextView[num_match + 1];
        for (int l = 1; l <= num_match; l++) {
            pairs[1].setTextSize(15);
            pairs[1].setLayoutParams(layoutParams);
            pairs[1].setId(l);
            linearLayout.addView(pairs[1]);

        }
    }
}

关联的 XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".InputPage2"
    android:background="#ffffff"
    tools:layout_editor_absoluteY="25dp"
    tools:layout_editor_absoluteX="0dp"
    android:backgroundTint="@color/BackgroundGrey">


    <Button
        android:onClick="toSaveField"
        android:id="@+id/saveButton1"
        android:layout_width="341dp"
        android:layout_height="70dp"
        android:background="@drawable/buttons"
        android:text="@string/saveButton"
        android:textSize="20dp"
        android:textStyle="bold"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintHorizontal_bias="0.666"
        app:layout_constraintVertical_bias="1.0"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />


    <EditText
        android:id="@+id/textNotAtHomes"
        android:layout_width="346dp"
        android:layout_height="477dp"
        android:ems="10"
        android:background="@color/hintTextColour"
        android:inputType="textMultiLine"
        android:hint="@string/hintText2"
        android:textColorHint="@color/hintTextColour2"
        android:layout_marginLeft="8dp"
        android:gravity="fill_horizontal"
        android:padding="6dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintVertical_bias="0.303" />

    <TextView
        android:id="@+id/ListName"
        android:layout_width="344dp"
        android:layout_height="37dp"
        android:textColor="@color/blackText"
        android:background="@color/whiteBackground"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintVertical_bias="0.0" />


</android.support.constraint.ConstraintLayout>

感谢任何帮助。

【问题讨论】:

标签: java android xml class button


【解决方案1】:

你可以像这样在活动之间传递数据

Intent intent = new Intent(activity2.this, activity1.class);
intent.putExtra("message", message);
startActivity(intent);

并通过其他活动获取它

 Bundle bundle = getIntent().getExtras();
 String message = bundle.getString("message");

【讨论】:

  • @The Abstract Lightbulb,因为你是新人..你需要确定你需要做的确切事情..获取数据并显示对你来说是可以的。但您遇到的真正问题是如何将数据从一个活动传递到另一个活动。
  • 谢谢你。至少你实际上试图帮助我解决我的问题。真的很感激。
  • @The Abstract Lightbulb,如果这对你有用,你可以接受我的问题。
  • 对不起大家。我的问题已经进行了一些编辑,所以感谢编辑的人可能更容易理解。
  • 嗨@Shamitha Silva。我尝试了您的建议,它能够将消息从一个类传递到另一个类,然后我使用以下语法尝试显示该消息。 TextView txt1 =(TextView)findViewById(R.id.ListName);`txt1.setText(name);`.但是当我尝试运行我的活动时,我得到了这个错误。
猜你喜欢
  • 2020-03-11
  • 1970-01-01
  • 1970-01-01
  • 2015-12-25
  • 1970-01-01
  • 2015-06-30
  • 1970-01-01
  • 2019-04-27
  • 1970-01-01
相关资源
最近更新 更多