문제

I have a button with id "ok" in my dialog.

Partial - Dialog Layout:

<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>

In activity:

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;
    }

How would I write onClick() event for this button?

도움이 되었습니까?

해결책

To initialize the Button, you need to use the Dialog object

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

Then set OnClickListener to the Button using setOnClickListener() as follows...

btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        //write your code here
    }
});

다른 팁

I would guess you make the method:

public void closeDialog(View v){ // Do what you want to do here (event handler)

}

?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top