在键入时更新用户输入

时间:2017-11-17 19:27:03

标签: java android xml

此问题与使用电子邮件意图的基本应用有关。

应用程序的UI显示4个输入:

  1. 电子邮件地址
  2. 主题
  3. 名称
  4. 消息
  5. 按钮
  6. 消息属于textMultiLine输入类型。

    该邮件的默认文字为:"Hello CustomerName (This is a dynamic variable that would update as the user types their name in input number 3), could you please review our app..."

    我要做的是将CustomerName实时更新为实际客户名称,因为他们在输入数字3(姓名)中输入了该名称。

    来源:

    package com.example.apit.testemailintent;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class MainActivity extends AppCompatActivity {
    
        EditText receiver, sub, mesg;
        EditText customerName;
        Button btn;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            receiver = (EditText) findViewById(R.id.email);
            sub = (EditText) findViewById(R.id.subject);
            customerName = (EditText) findViewById(R.id.name);
            mesg = (EditText) findViewById(R.id.message);
            btn = (Button) findViewById(R.id.submitButton);
    
            addListenerOnButton1();
    
        }
    
        public void addListenerOnButton1() {
            btn.setOnClickListener(new View.OnClickListener() {
    
    
                @Override
                public void onClick(View view) {
    
                    String Sendto = receiver.getText().toString();
                    String subject = sub.getText().toString();
                    String cusName = customerName.getText().toString();
                    String mesgs = "Hello " + cusName + ", could you please review our app...?";
    
    
                    Intent email = new Intent(Intent.ACTION_SEND);
                    email.putExtra(Intent.EXTRA_EMAIL, new String[]{Sendto});
                    email.putExtra(Intent.EXTRA_SUBJECT, subject);
                    email.putExtra(Intent.EXTRA_TEXT, mesgs);
    
                    email.setType("message/rfc822");
                    startActivity(Intent.createChooser(email, "Please Choose an Email Client"));
    
                }
            });
    
        }
    
    }
    

    谢谢,

1 个答案:

答案 0 :(得分:1)

nameEditText.addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable s) {}

   @Override
   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   @Override
   public void onTextChanged(CharSequence s, int start,int before, int count) {
      messageEditText.setText("Hello " + s + " could you please review our app")
   }
  });