为android自定义对话框按钮编写onClick()事件

时间:2014-04-16 02:24:42

标签: android dialog onclick

我有一个id为#34; ok"在我的对话框中。

部分 - 对话框布局:

<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_margin="8dp"
            android:layout_below="@+id/licenseTypeLayout">

        <Button
                android:id="@+id/ok"
                style="@style/agreement_text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"                   
                android:text="Ok"/>        
    </LinearLayout>

活动:

Dialog dialog;
    @OptionsItem(R.id.action_about)
    boolean displayPopup() {
        dialog = new Dialog(this);
        dialog.setContentView(R.layout.about_dialog);
        dialog.setTitle(R.string.app_name);          
        dialog.show();
        Button btn=(Button)findViewById(R.id.ok);

        //btn remains empty

        return true;
    }

如何为此按钮编写onClick()事件?

2 个答案:

答案 0 :(得分:2)

要初始化Button,您需要使用Dialog对象

Button btn = (Button) dialog.findViewById(R.id.ok);

然后使用OnClickListenerButton设置为setOnClickListener(),如下所示......

btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        //write your code here
    }
});

答案 1 :(得分:1)

我猜你会做出这个方法:

public void closeDialog(View v){    //在这里做你想做的事(事件处理程序)

}

相关问题