2 回答
TA贡献1798条经验 获得超7个赞
根据 Sibin Davis 先生和 Karan Sandhu 先生的说法,这是最终代码
btnA1.setOnLongClickListener(new Button.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(getApplicationContext(),ttlA1,Toast.LENGTH_SHORT).show();
dialogForm(btnA1); // <---- add this
return true;
}
});
和函数 dialogForm() :
private void dialogForm(final Button b1){ //<-- add this
dialog = new AlertDialog.Builder(MainActivity.this);
inflater = getLayoutInflater();
dialogView = inflater.inflate(R.layout.macro_form, null);
dialog.setView(dialogView);
dialog.setCancelable(true);
dialog.setIcon(R.mipmap.ic_launcher);
dialog.setTitle("Macro Form");
txtMacroName = (EditText) dialogView.findViewById(R.id.txtMacroName);
txtMacroStatus = (EditText) dialogView.findViewById(R.id.txtMacroValue);
dialog.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
macroName = txtMacroName.getText().toString();
macroStatus = txtMacroStatus.getText().toString();
b1.setText(macroName); // <---- add this
dialog.dismiss();
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();
}
谢谢大家
TA贡献1796条经验 获得超4个赞
尝试为您的按钮提供 id,这样当用户选择一个按钮并键入文本时,您可以在对话框中将该文本设置为该特定按钮并关闭它。然后在设置按钮文本
dialog.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
macroName = txtMacroName.getText().toString();
macroStatus = txtMacroStatus.getText().toString();
dialog.dismiss();
}
});
在 setPositivebutton 方法中,只需获取 edittext 的值(即 getText().toString),然后将其设置为按钮,如 btn.setText(edittext 的值)。
添加回答
举报