ボタンをクリックして、alertdialogのEditTextからアクティビティのEditTextにテキストをコピーして貼り付けるにはどうすればよいですか?

チャリス

私は検索が、私はちょうどに対処見ることができる質問copy、あるいはcopying to clipboard単に、またはpasting具体的に私が欲しいもの(1回のボタンクリックで、PositiveButton中にはAlertDialog)でユーザーが入力したテキストをコピーすることである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;

  }
});
アブドムン

私はあなたがそれをするためにこの簡単なコード例を書きました。

アクティビティのエディットテキストの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);
}
}

これがお役に立てば幸いです

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ