如何在多个其他活动中使用Edittext

时间:2016-12-16 12:43:20

标签: android

我有一个edittext,您可以在其中输入您的姓名,因此该名称可用于新活动。但是当我进入下一页时,我收到了一个错误。

使用edittext的活动代码:

 public class aanmeldbalie extends AppCompatActivity {

    EditText SendValue;
    Button SendEditTextValue;
    Intent intent;

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

        SendEditTextValue = (Button)findViewById(R.id.buttonAanmeldbalie);
        SendValue = (EditText)findViewById(R.id.kandidaatsnaam);

        SendEditTextValue.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                intent = new Intent(getApplicationContext(),wachtkamer.class);
                intent.putExtra("EdiTtEXTvALUE", SendValue.getText().toString());
                startActivity(intent);

            }
        });

    }

    public void sendMessage(View view) {
        Intent wachtkamer = new Intent(this, wachtkamer.class);
        startActivity(wachtkamer);
    }

我们想要使用edittext的活动代码:

    TextView receive;

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

        receive = (TextView)findViewById(R.id.kandidaatsnaam);

        receive.setText(getIntent().getStringExtra("EdiTtEXTvALUE"));
    }

    public void sendMessage(View view) {
        Intent uitleg_zuster = new Intent(this, uitleg_zuster.class);
        startActivity(uitleg_zuster);
    }

edittext的XML代码:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Naam"
    android:layout_below="@id/tekstvak1"
    android:paddingLeft="16dp"
    android:background="#B3FFFFFF"
    android:padding="8dp"
    android:id="@+id/kandidaatsnaam"
    />

2 个答案:

答案 0 :(得分:0)

Remove the below code:-

**

    SendEditTextValue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent = new Intent(getApplicationContext(),wachtkamer.class);
                intent.putExtra("EdiTtEXTvALUE", SendValue.getText().toString());
                startActivity(intent);
            }
        });

**
And add the below make sendMessage() like this:


public void sendMessage(View view) {
    Intent uitleg_zuster = new Intent(this, uitleg_zuster.class);
intent.putExtra("EdiTtEXTvALUE", SendValue.getText().toString());
    startActivity(uitleg_zuster);
}

Make sure u have android:onClick="sendMessage" in xml file

<Button
  android:text="send"
  android:onClick="sendMessage"/>

答案 1 :(得分:0)

使onclick像这样,不需要发送消息功能。

SendEditTextValue.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            intent = new Intent(aanmeldbalie.this,wachtkamer.class);
            intent.putExtra("EdiTtEXTvALUE", SendValue.getText().toString());
            startActivity(intent);

        }
    });

在你的第二个活动中

receive.setText(getIntent().getStringExtra("EdiTtEXTvALUE"));

将这段代码放在try catch中或检查null值以防止空指针异常。