[英]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
. 我搜寻了一下,但看到的问题只是copy
, copying to clipboard
或pasting
。 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次按钮点击后, PositiveButton
在AlertDialog
)是由用户在输入的文本复制EditText
我的alertdialog
到EditText
我的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.