按钮点击事件未触发

Button click event not getting triggered

我在 table 布局中定义了一组按钮。

custom_layout.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:id="@+id/tableLayout">

    <TableRow
        android:layout_weight="1">
        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/firstBtn"
            android:text="F"
            android:layout_weight="0.3">
        </Button>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/secondBtn"
            android:text="S"
            android:layout_weight="0.3">
        </Button>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/thirdBtn"
            android:text="T"
            android:layout_weight="0.3">
        </Button>

    </TableRow>

    </TableLayout>

</RelativeLayout>

此 XML 设置为自定义警报对话框中的布局。

代码:

public class CustomDialogPreference extends DialogPreference {


private Button firstBtn = null;
private Button secondBtn = null;
private Button thirdBtn = null;


public CustomDialogPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    setPersistent(false);
    this.context = context;
    setDialogLayoutResource(R.layout.custom_layout);



}

@Override
protected void onBindDialogView(View view) {

    firstBtn = (Button)view.findViewById(R.id.firstBtn);
    secondBtn = (Button)view.findViewById(R.id.secondBtn);
    thirdBtn = (Button)view.findViewById(R.id.thirdBtn);

    firstBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.d("Check","First button is clicked"));

        }
    });

    secondBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.d("Check","Second button is clicked"));

        }
    });

    thirdBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.d("Check","Third button is clicked"));

        }
    });


    super.onBindDialogView(view);
}

@Override
protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);
    persistBoolean(positiveResult);

    Log.d("Blockout","Dialog close detected");
    if(positiveResult)
    {

        Log.d("Blockout","Save button had been clicked");

    }

   }

}

打开对话框并单击按钮时,我看不到相应的日志消息。我也没有收到任何空指针异常,所以我认为视图已正确设置和访问。知道我错过了什么吗?

如果您想要自定义对话框,我建议您使用 DialogFragment class。有关于这些的文档指南 - Dialogs.

我想你需要这个方法 - onCreateDialogView

例如,

public class CustomDialogPreference extends DialogPreference 
    implements View.OnClickListener {

    private Context context;

    public CustomDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        setPersistent(false);
        this.context = context;
    }

    @Override
    protected View onCreateDialogView() {
        super.onCreateDialogView();
        LayoutInflater inflater = LayoutInflater.from(this.context);
        View rootView = inflater.inflate(R.layout.custom_layout,null);

        rootView.findViewById(R.id.firstBtn).setOnClickListener(this);
        rootView.findViewById(R.id.secondBtn).setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.firstBtn:
                Log.d("Check","First button is clicked"));
                break;
            case R.id.secondBtn:
                break;
        }
    }