如何从**编辑文本中获取价值**并在另一项活动

时间:2017-05-22 04:25:12

标签: java android xml class button

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

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

我希望能够将用户输入的文字带到@+id/location活动的InputPage Edittext框中,并将其显示在@+id/ListName InputPage2 TextView上按下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 code **

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>

感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

您可以在此类活动之间传递数据

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

并通过其他活动获取

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