按钮在长按按钮后出现的自定义对话框内不起作用

时间:2017-07-19 14:56:41

标签: android button onclicklistener buttonclick customdialog

我在按钮的longPress中打开了一个自定义对话框, 在该自定义对话框中,我有两个按钮和一个编辑文本。我想更改我用自定义对话框中的编辑文本获得的名称所表示的按钮的名称,按钮不起作用。

public void initializeLPButtons(Button[] btns, int[] rArrays) {

    for(i=0; i<btns.length; i++) {

        btns[i] = (Button) findViewById(rArrays[i]);
        btns[i].setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {

                renameBtn();
                return true;
            }
        });
    }
}

public void renameBtn() {

    final AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
    dialog.setView(R.layout.dialog_renamebtn);
    dialog.setTitle("Rename Button...");
    dialog.setMessage("Rename The Button To:");

    LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
    final View dialogView = inflater.inflate(R.layout.dialog_renamebtn, null);

    renameEt = (EditText) dialogView.findViewById(R.id.renameEt);
    renameBtn = (Button) dialogView.findViewById(R.id.renameBtn);
    cnclRenameBtn = (Button) dialogView.findViewById(R.id.cnlRenameBtn);

    cnclRenameBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }
    });

    renameBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            renameBtnTxt();
        }
    });

    dialog.show();
}

public void renameBtnTxt() {

    if (renameEt.getText().toString().length() > 0) {
        btns[i].setText(renameEt.getText().toString());
    }else {
        Toast.makeText(MainActivity.this, "Please choose a name.",
                Toast.LENGTH_SHORT).show();
    }
}

我不认为我的代码有问题。如果它是java帮助我找到它。

1 个答案:

答案 0 :(得分:1)

像这样创建对话框:

private void CreateGetPhoneNumDialog() {
        Dialog PhoneNumberDialog = new Dialog(getActivity());
        PhoneNumberDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        PhoneNumberDialog.setContentView(R.layout.mobile_number_pop_message);
        PhoneNumberDialog.setCancelable(false);
}

创建对话框后获取edittext和按钮的引用

etDialogPhoneNum = (EditText) PhoneNumberDialog.findViewById(R.id.et_DialogCustomerPhone);
btnGetPhoneNum = (Button) PhoneNumberDialog.findViewById(R.id.btn_DialogGetPhoneNum);

然后在对话框的按钮

上设置点击监听器
btnGetPhoneNum.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           // Do your work here
       }
});

mobile_number_pop_message.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="300dp"
    android:background="@color/White"
    android:layout_height="wrap_content"
    >

     <EditText
        android:layout_width="match_parent"
        android:layout_height="53dp"
        android:id="@+id/et_DialogCustomerPhone"
        android:inputType="phone"
        android:background="@null"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:maxLength="9"
        android:hint="512345678"
        android:gravity="start|center_vertical"
        android:layout_gravity="start|center_vertical"
    />

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="@string/str_UpdatePhoneNumber"
        android:textAllCaps="true"
        android:layout_gravity="center"
        android:id="@+id/btn_DialogGetPhoneNum" 
    />

</LinearLayout>

修改

并显示如下对话框:

PhoneNumberDialog.show();