代码之家  ›  专栏  ›  技术社区  ›  CHarris

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

  •  0
  • CHarris  · 技术社区  · 7 年前

    我搜索了一下,但我能看到的问题处理得很好 copy 或 copying to clipboard ,或只是 pasting PositiveButton 在里面 AlertDialog )是复制用户在中输入的文本 EditText 我的 alertdialog 到 我的 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;
    
      }
    });
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Abdomns    7 年前

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

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

    希望这对你有帮助