监听Dialog中的按钮

很多情况下我们在使用自定义的Dialog时需要监听Dialog中的按钮,如果将Activity和Dialog中的点击事件分开写会比较麻烦,而且点击事件中可能需要调用Activity中的变量或方法,所以在Activity中监听Dialog(或者其它Activity)中的按钮是很常见的。以下是可行的几种方法:

 
一、将Activity中的点击事件监听器传给MyDialog:
这里让MainActivity实现了OnClickListener接口,所以MainActivity的实例instance就是一个点击事件监听器。
类似的,如果MainActivity没有实现OnClickListener接口,可以定义一个OnClickListener接口类型的对象mListener(将其定义为MainActivity的成员变量),将mListener传给MyDialog即可。
MainActivity.java代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
 
package com.example.dialog;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Toast;

public  class MainActivity  extends Activity  implements OnClickListener {
    
     private  static MainActivity instance;
     private MyDialog dialog;

    @Override
     protected  void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        instance =  this;
        setContentView(R.layout.main);
        
        findViewById(R.id.btn_dialog).setOnClickListener( this);
        
        dialog =  new MyDialog( this, R.style.Dialog);
    }
    
     public  static MainActivity getInstance() {
         return instance;
    }

    @Override
     public  void onClick(View v) {
         switch (v.getId()) {
         case R.id.btn_dialog:
            showDialog();
             break;
         case R.id.btn_dialog_cancel:
            Toast.makeText( this"cancel", Toast.LENGTH_SHORT).show();
             break;
         case R.id.btn_dialog_certain:
            Toast.makeText( this"certain", Toast.LENGTH_SHORT).show();
             break;
        }
    }

     private  void showDialog() {
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams params = dialogWindow.getAttributes();
        params.width = LayoutParams.WRAP_CONTENT;
        params.height = LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;
        
        dialogWindow.setAttributes(params);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }
    
}

MyDialog.java代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
package com.example.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;

public  class MyDialog  extends Dialog {

     public MyDialog(Context context,  int theme) {
         super(context, theme);
    }
    
    @Override
     protected  void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);
        
        findViewById(R.id.btn_dialog_cancel).setOnClickListener(
                MainActivity.getInstance());
        findViewById(R.id.btn_dialog_certain).setOnClickListener(
                MainActivity.getInstance());
    }

}
 
 
二、创建一个OnDialogButtonClickListener接口:
让MyDialog类实现OnClickListener接口,并且在onClick(View v)方法中调用接口中的方法onDialogButtonClick(View v)。在MainActivity中实现该接口,重写onDialogButtonClick(View v)方法,最后给dialog设置监听器将OnDialogButtonClickListener接口的实例传到MyDialog类中即可。
MainActivity.java代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 
package com.example.dialog;

import com.example.dialog.MyDialog.OnDialogButtonClickListener;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.Toast;

public  class MainActivity  extends Activity {
    
     private Button button;
     private MyDialog dialog;

    @Override
     protected  void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        button = (Button) findViewById(R.id.btn_dialog);
        button.setOnClickListener( new OnClickListener() {
            
            @Override
             public  void onClick(View v) {
                showDialog();
            }
        });     
        
        dialog =  new MyDialog( this, R.style.Dialog);
        dialog.setOnDialogButtonClickListener( new OnDialogButtonClickListener() {
            
            @Override
             public  void onDialogButtonClick(View v) {
                 switch (v.getId()) {
                 case R.id.btn_dialog_cancel:
                    Toast.makeText(MainActivity. this"cancel",
                            Toast.LENGTH_SHORT).show();
                     break;
                 case R.id.btn_dialog_certain:
                    Toast.makeText(MainActivity. this"certain",
                            Toast.LENGTH_SHORT).show();
                     break;
                }
            }
        });
    }
    
     private  void showDialog() {
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams params = dialogWindow.getAttributes();
        params.width = LayoutParams.WRAP_CONTENT;
        params.height = LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;
        
        dialogWindow.setAttributes(params);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }
    
}
MyDialog.java代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
package com.example.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public  class MyDialog  extends Dialog  implements OnClickListener {
    
     private OnDialogButtonClickListener listener;

     public MyDialog(Context context,  int theme) {
         super(context, theme);
    }
    
    @Override
     protected  void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);
        
        findViewById(R.id.btn_dialog_cancel).setOnClickListener( this);
        findViewById(R.id.btn_dialog_certain).setOnClickListener( this);
    }

    @Override
     public  void onClick(View v) {
        listener.onDialogButtonClick(v);
    }
    
     public  void setOnDialogButtonClickListener(OnDialogButtonClickListener listener) {
         this.listener = listener;
    }
    
     public  interface OnDialogButtonClickListener {
         public  void onDialogButtonClick(View v);
    }

}
 
附:main.xml代码:
1
2
3
4
5
6
7
8
9
10
11
 
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
    android:layout_width= "match_parent"
    android:layout_height= "match_parent"  >

     <Button
        android:id
= "@+id/btn_dialog"
        android:layout_width= "wrap_content"
        android:layout_height= "wrap_content"
        android:text= "@string/btn_dialog"  />

</LinearLayout>
dialog.xml代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
<?xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
    android:layout_width= "match_parent"
    android:layout_height= "match_parent"
    android:background= "@drawable/dialog_bg"
    android:gravity= "center"
    android:orientation= "vertical"  >

     <ImageView
        android:layout_width
= "wrap_content"
        android:layout_height= "wrap_content"
        android:src= "@drawable/ic_launcher"  />

     <LinearLayout
        android:layout_width
= "match_parent"
        android:layout_height= "wrap_content"
        android:gravity= "center"
        android:orientation= "horizontal"  >

         <Button
            android:id
= "@+id/btn_dialog_cancel"
            android:layout_width= "wrap_content"
            android:layout_height= "wrap_content"
            android:text= "@string/btn_cancel"  />

         <Button
            android:id
= "@+id/btn_dialog_certain"
            android:layout_width= "wrap_content"
            android:layout_height= "wrap_content"
            android:text= "@string/btn_certain"  />
     </LinearLayout>

</LinearLayout>
 
 
 
三、定义实现OnClickListener接口的超类ClickActivity:
多个Activity通过继承一个实现了OnClickListener接口的父类从而将点击事件写在同一个类中,此方法适用于多个Activity的情况。
ClickActivity.java代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
package com.example.dialog;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class ClickActivity extends Activity implements OnClickListener {

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_one:
            Toast.makeText(this,  "来自ActivityOne", Toast.LENGTH_SHORT).show();
            break;
        case R.id.btn_two:
            Toast.makeText(this,  "来自ActivityTwo", Toast.LENGTH_SHORT).show();
            break;
        }
    }

}
ActivityOne.java代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
package com.example.dialog;

import android.os.Bundle;

public class ActivityOne extends ClickActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.one);
        
        findViewById(R.id.btn_one).setOnClickListener(this);
    }
    
}
ActivityTwo.java代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
package com.example.dialog;

import android.os.Bundle;

public class ActivityTwo extends ClickActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.two);
        
        findViewById(R.id.btn_two).setOnClickListener(this);
    }
    
}
 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值