简体   繁体   English

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

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

I searched but the questions I could see dealt with just copy , or copying to clipboard , or just pasting . 我搜寻了一下,但看到的问题只是copycopying to clipboardpasting Specifically what I want (in 1 button click, the PositiveButton in AlertDialog ) is to copy the text entered by user in the EditText of my alertdialog to the EditText of my Activity . 具体来说,我想要的东西(在1次按钮点击后, PositiveButtonAlertDialog )是由用户在输入的文本复制EditText我的alertdialogEditText我的Activity

Can you tell me how to do this please? 你能告诉我怎么做吗? Here is the code I am using and trying to fix: 这是我正在使用并尝试修复的代码:

//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;

  }
});

i wrote this simple code example for you to do it. 我为您编写了这个简单的代码示例。

just add method to setText on Your edittext in your Activity: 只需在Activity中的edittext上向setText添加方法:

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

when user click in dialog get text from edittext dialog and pass using this method: 当用户单击对话框时,从edittext对话框获取文本并使用此方法传递:

setTextFromDialog(YouEditTextValueX);

here code example: 这里的代码示例:

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);
}
}

hope this help you 希望这对你有帮助

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

相关问题 如何显示 EditText 中的文本? - How do I display text from an EditText? 如何从EditText中删除文本 - How do I remove text from EditText 如何检查我的 editText 字段是否为空并使其转到带有 Button 的新活动? - How do I check if my editText field is empty and make it go to a new activity with a Button? 单击按钮从Android中的EditText获取文本 - getting text from EditText in Android on click of button 如何根据 activity_main 中编辑文本中的文本更改片段中的 imageview 图像 - How can I change the imageview image in a fragment based on the text in a edittext from my activity_main 如何使MainActivity类中的AlertDialog中的EditText中的文本可用? - How to make text from EditText in AlertDialog available in MainActivity class? 将EditText中的文本控制到AlertDialog中 - Control text in EditText into AlertDialog 如何获取AlertDialog setPositiveButton从editText中提取文本并将其放置在listView中 - How to get AlertDialog setPositiveButton to pull text from editText and place in listView 如何在EditText获得焦点后立即显示AlertDialog? - How do I make my AlertDialog appear as soon as EditText has focus? Android:如何在自定义AlertDialog中检索Edittext.getText()? - Android: How do I retrieve Edittext.getText() in custom AlertDialog?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM