如何在android中实现OnCancel监听器

时间:2011-11-18 18:36:18

标签: android

public class ModalityDialog extends Dialog implements OnClickListener, OnCancelListener {
EditText capacity ;
EditText color ;
Button modal_ok ;

String Capacity;
String Color;

public ModalityDialog(Context context) {
    super(context);
    /** It will hide the title */
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.preferences);

    capacity = (EditText)findViewById(R.id.capacityInput);      
    color = (EditText)findViewById(R.id.ColorInput);        
    Button modal_ok = (Button)findViewById(R.id.modality_ok);
    modal_ok.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    if (v == modal_ok){

        cancel();
    }
}

@Override
public void onCancel( DialogInterface dialog ){
    if( dialog == this){
        Capacity = capacity.getText().toString();           
        Color = color.getText().toString();

    }
}


 }

我将其称为如下,其中modal是一个按钮

modal.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ModalityDialog modalDialog = new ModalityDialog(InputInfo.this);
            modalDialog.show();}
    });

我收到以下错误

11-19 03:30:00.733: E/AndroidRuntime(305): FATAL EXCEPTION: main
11-19 03:30:00.733: E/AndroidRuntime(305): java.lang.NullPointerException
11-19 03:30:00.733: E/AndroidRuntime(305):  at com.domain.ActivityEx.ModalityDialog.   <init>(ModalityDialog.java:38)
11-19 03:30:00.733: E/AndroidRuntime(305):  at  com.domain.ActivityEx.InputInfo$1.onClick(InputInfo.java:57)
11-19 03:30:00.733: E/AndroidRuntime(305):  at android.view.View.performClick(View.java:2408)
11-19 03:30:00.733: E/AndroidRuntime(305):  at android.view.View$PerformClick.run(View.java:8816)
11-19 03:30:00.733: E/AndroidRuntime(305):  at android.os.Handler.handleCallback(Handler.java:587)
11-19 03:30:00.733: E/AndroidRuntime(305):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-19 03:30:00.733: E/AndroidRuntime(305):  at android.os.Looper.loop(Looper.java:123)
11-19 03:30:00.733: E/AndroidRuntime(305):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-19 03:30:00.733: E/AndroidRuntime(305):  at java.lang.reflect.Method.invokeNative(Native Method)
11-19 03:30:00.733: E/AndroidRuntime(305):  at java.lang.reflect.Method.invoke(Method.java:521)
11-19 03:30:00.733: E/AndroidRuntime(305):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-19 03:30:00.733: E/AndroidRuntime(305):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-19 03:30:00.733: E/AndroidRuntime(305):  at dalvik.system.NativeStart.main(Native Method)

我希望在调用onCancel函数后得到容量,颜色等变量的值。现在,在将值放入变量之前,对话框将挂起并完成。任何人都可以建议我该怎么做?

提前致谢

2 个答案:

答案 0 :(得分:0)

为什么在调用cancel()之前没有在onclick方法中取值?

答案 1 :(得分:0)

试试这个,看看这是不是你想要做的。

    @Override
        public void onClick(View v) {
            if (v == modal_ok){
                Capacity = capacity.getText().toString();                     
                Color = color.getText().toString();  

                cancel();
            }
        }

此外,使用小写的起始字母命名字段并使名称中的每个后续单词都具有capatilizes是一种公认​​的惯例。

例如:

String fooBar;
String theColorRed;