当用户长按时更改android上的任何按钮

我有一些按钮,比如 btnA1、btnA2、btnA3,我希望用户可以在长按时更改按钮文本。当用户长按任何按钮时,它会为用户提供对话框以填充按钮文本和一些 textView。这是我的代码:


btnA1.setOnLongClickListener(new Button.OnLongClickListener() {

  @Override

  public boolean onLongClick(View v) {

    dialogForm();

    return true;

  }

});

这是 dialogForm 函数:


private void dialogForm(){

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


    dialog.dismiss();

  }

});


dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

  @Override

  public void onClick(DialogInterface dialog, int which) {

    dialog.dismiss();

  }

});

dialog.show();

}

我如何检测在对话框dismiss()之前按下了哪个按钮,只有那个按钮会改变;


慕侠2389804
浏览 127回答 2
2回答

元芳怎么了

根据 Sibin Davis 先生和 Karan Sandhu 先生的说法,这是最终代码btnA1.setOnLongClickListener(new Button.OnLongClickListener() {&nbsp; @Override&nbsp; public boolean onLongClick(View v) {&nbsp; &nbsp; Toast.makeText(getApplicationContext(),ttlA1,Toast.LENGTH_SHORT).show();&nbsp; &nbsp; dialogForm(btnA1); // <---- add this&nbsp; &nbsp; return true;&nbsp; }});和函数 dialogForm() :private void dialogForm(final Button b1){ //<-- add thisdialog = 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&nbsp; &nbsp; = (EditText) dialogView.findViewById(R.id.txtMacroName);txtMacroStatus&nbsp; &nbsp; = (EditText) dialogView.findViewById(R.id.txtMacroValue);dialog.setPositiveButton("Submit", new DialogInterface.OnClickListener() {&nbsp; @Override&nbsp; public void onClick(DialogInterface dialog, int which) {&nbsp; &nbsp; macroName&nbsp; &nbsp; = txtMacroName.getText().toString();&nbsp; &nbsp; macroStatus&nbsp; &nbsp; = txtMacroStatus.getText().toString();&nbsp; &nbsp; b1.setText(macroName); // <---- add this&nbsp; &nbsp; dialog.dismiss();&nbsp; }});dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {&nbsp; @Override&nbsp; public void onClick(DialogInterface dialog, int which) {&nbsp; &nbsp; dialog.dismiss();&nbsp; }});dialog.show();}谢谢大家

慕的地8271018

尝试为您的按钮提供 id,这样当用户选择一个按钮并键入文本时,您可以在对话框中将该文本设置为该特定按钮并关闭它。然后在设置按钮文本dialog.setPositiveButton("Submit", new DialogInterface.OnClickListener() {&nbsp; @Override&nbsp; public void onClick(DialogInterface dialog, int which) {&nbsp; &nbsp; macroName&nbsp; &nbsp; = txtMacroName.getText().toString();&nbsp; &nbsp; macroStatus&nbsp; &nbsp; = txtMacroStatus.getText().toString();&nbsp; &nbsp; dialog.dismiss();&nbsp; }});在 setPositivebutton 方法中,只需获取 edittext 的值(即 getText().toString),然后将其设置为按钮,如 btn.setText(edittext 的值)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java