Android界面之—-自定義的Dialog,然後利用回調方法,在調用處,進行Dialog中各按鈕的事件處理

package com.demo; 
 
import android.app.Dialog; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
 
public class LeaveMeetingDialog extends Dialog  implements OnClickListener{ 
 
    private Button  quitBtn,stopBtn,cancelBtn; 
    private LeaveMeetingDialogListener listener; 
 
    public interface LeaveMeetingDialogListener{ 
        public void onClick(View view); 
    } 
 
    public LeaveMeetingDialog(Context context,int theme,LeaveMeetingDialogListener listener) { 
        super(context,theme); 
    } 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.leave_meeting_dialog); 
        initViews(); 
    } 
 
    private void initViews(){ 
        quitBtn = (Button)findViewById(R.id.quit_btn); 
        stopBtn = (Button)findViewById(R.id.stop_btn); 
        cancelBtn = (Button)findViewById(R.id.cancel_btn); 
 
        quitBtn.setOnClickListener(this); 
        stopBtn.setOnClickListener(this); 
        cancelBtn.setOnClickListener(this); 
    } 
 
    @Override 
    public void onClick(View v) { 
        listener.onClick(v); 
    } 
 

—調用處:

[java]
LeaveMeetingDialog  dialog = new LeaveMeetingDialog(this,R.style.Theme_CustomDialog, 
            new LeaveMeetingDialogListener() { 
        @Override 
        public void onClick(View view) { 
            switch(view.getId()){ 
                case R.id.quit_btn:break; 
                case R.id.stop_btn:break; 
                case R.id.cancel_btn:break; 
            } 
        } 
}); 
      dialog.show(); 

 

 

摘自 liuxiIT的專欄

發佈留言