繁体   English   中英

单击按钮后,如何将文本从alertdialog的EditText复制并粘贴到活动的EditText中?

[英]How do I copy and paste text from EditText of alertdialog to EditText of my activity, on button click?

我搜寻了一下,但看到的问题只是copycopying to clipboardpasting 具体来说,我想要的东西(在1次按钮点击后, PositiveButtonAlertDialog )是由用户在输入的文本复制EditText我的alertdialogEditText我的Activity

你能告诉我怎么做吗? 这是我正在使用并尝试修复的代码:

//when user touches on "commentname" edittext we want the alertdialog to open
commentname.setOnTouchListener(new View.OnTouchListener() {
  @Override
  public boolean onTouch(View v, MotionEvent event) {

    if(event.getAction() == MotionEvent.ACTION_UP) {
      AlertDialog.Builder builder = new AlertDialog.Builder(NewContact.this);
      builder.setTitle("Ur Comment:");


      //start the following xml file/ layout
      View viewInflated = LayoutInflater.from(NewContact.this).inflate(R.layout.comment_text_pop_up, null, false);
      builder.setView(viewInflated);

      // Set up the buttons
      builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

          dialog.dismiss();
          //we want to copy the text entered in "input", in the alertdialog, 
          //and paste it in commentname
          commentname.setText(alertdialog_edittext.getText().toString());
        }
      });
      builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
          dialog.cancel();
        }
      });

      AlertDialog dialog = builder.create();
      alertdialog_edittext = (EditText) dialog.findViewById(R.id.input);

      dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

      dialog.show();
      return true;

    }
    return false;

  }
});

我为您编写了这个简单的代码示例。

只需在Activity中的edittext上向setText添加方法:

private void setTextFromDialog(final String textFromDialog){
    myEditText.setText(textFromDialog);
}

当用户单击对话框时,从edittext对话框获取文本并使用此方法传递:

setTextFromDialog(YouEditTextValueX);

这里的代码示例:

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
private EditText myEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button ShowDialog = findViewById(R.id.showdialog_id);
    myEditText = findViewById(R.id.editText_id);

    ShowDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
            final EditText edittext = new EditText(MainActivity.this);
            alert.setTitle("Title");
            alert.setMessage("Message");
            alert.setView(edittext);
            alert.setPositiveButton("Set text", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String YouEditTextValueX = edittext.getText().toString();
                    if(YouEditTextValueX.length() > 0){

                        //this line for call method and pass the text
                        setTextFromDialog(YouEditTextValueX);
                    }
                }
            });
            alert.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // what ever you want to do with No option.
                }
            });
            alert.show();
        }
    });
}
private void setTextFromDialog(final String textFromDialog){
    myEditText.setText(textFromDialog);
}
}

希望这对你有帮助

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM