2014-04-22 3 views
2

저는 모바일 프로그래밍에 익숙하지 않고 지금은 약간의 어려움을 겪고 있습니다. DialogPrefence으로 비밀번호를 설정하기 위해 무언가를 쓰려고합니다. 제 질문은 대화 상자에서 OK에있는 OnClick 이벤트를 어떻게 받습니까?OnClick에서 작동하도록 DialogPreference POSITIVE_BUTTON을 얻는 방법은 무엇입니까?

package com.kontrol.app; 

import android.content.Context; 
import android.content.DialogInterface; 
import android.preference.DialogPreference; 
import android.util.AttributeSet; 

public class SS1_Senha extends DialogPreference implements DialogInterface.OnClickListener{ 

    public SS1_Senha(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setPersistent(false); 
     setDialogLayoutResource(R.layout.ss1_senha); 

     setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       //Action after OK 

      } 
     }); 


    } 
} 

답변

12

는이

public class CustomDialogPreference extends DialogPreference implements DialogInterface.OnClickListener{ 

public CustomDialogPreference(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    setPersistent(false); 
    setDialogLayoutResource(R.layout.image_dialog); 
    setPositiveButtonText("OK"); 
    setNegativeButtonText("CANCEL"); 
} 

@Override 
public void onClick(DialogInterface dialog, int which){ 
    if(which == DialogInterface.BUTTON_POSITIVE) { 
     // do your stuff to handle positive button 
    }else if(which == DialogInterface.BUTTON_NEGATIVE){ 
     // do your stuff to handle negative button 
    } 
} 
} 
+0

감사처럼 많은 사용자 정의 DialogPreference 클래스를 작성 DialogInterface.OnClickListener를 구현하고 각 버튼

OnClick 이벤트를 처리 할 필요가! 그것은 매력처럼 작동합니다! –

+0

setNegativeButtonText ("CANCEL"); – Pawel

0

당신이에 AlertDialog.Builder를 사용할 수있는 경고 대화 상자를 표시하려면 :

여기 내 코드입니다. 예를 들어 : 내가 제대로 이해한다면

AlertDialog alertDialog = new AlertDialog.Builder(
         AlertDialogActivity.this).create(); 

    // Setting Dialog Title 
    alertDialog.setTitle("Alert Dialog"); 

    // Setting Dialog Message 
    alertDialog.setMessage("My Message"); 


    // Setting OK Button 
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
      // Write your code here to execute after dialog closed 
      Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show(); 
      } 
    }); 

    // Showing Alert Message 
    alertDialog.show(); 
0

, 당신은 다른 클래스 (안 SS1_Senha에서)에서 키 프레스 이벤트에 대해 알 필요가있다. 이를 위해 리스너 (관찰자) 패턴 또는 핸들러를 사용할 수 있습니다.

관련 문제