android - 按钮点击事件未触发

标签 android android-layout android-preferences android-dialog android-tablelayout

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

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

    }

   }

}

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

最佳答案

如果您想要自定义对话框,我建议您使用 DialogFragment 类。有关于这些的文档指南 - 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;
        }
    }

关于android - 按钮点击事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41079356/

相关文章:

android - 如何从 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 恢复?

android - 自定义 FragmentTabHost 在底部设置 TabWidget

android - 默认情况下,PreferenceFragmentCompat 的 iconSpaceReserved 为 true

java - Android Oreo 通知崩溃系统 UI

android - cookie 不是通过移动数据接收的,而是在 android 注册期间通过 wifi 接收的(设备是 sony xperia)

java - 如何将按钮背景更改为图像,然后在按下时返回默认按钮

java - 滚动到 ListPreference 中的选定项目

java - RxJava - Observable 的 zip 列表

Android Studio 布局 - 在同一行上彼此相邻的两个 TextView 的父级中水平居中

android - 为什么 SwitchPreference 在从开到关切换时不显示动画,反之亦然?