2013-08-26 3 views
0

사용자 대화 상자를 만들 수 있지만 일반 대화 상자가 아닌 버튼 중 하나가 기본 응답 인 이고 버튼이 켜지는 버튼이 있습니다. 그 카운트 다운 시간.기본 버튼 카운트 다운이있는 대화 버튼

예를 들어

:

대화 : "안녕하세요 .............. 당신이 할 수있다?" (예/아니오)

아니요 초 후에 자동으로 수락됩니다. 이 같은

:

이 하나 더 예를입니다 like this

답변

2

이 완벽한 해답이 될 수 있습니다,하지만 난 당신을 위해 작동합니다 생각하고 당신은 당신의 코드를 사용자 정의 할 수 있습니다 필요에 따라.

dailog 사용자 정의보기가있는 레이아웃을 생성, 그것은 (my_dialog_layout.xml을) 이름 :

<?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:orientation="vertical" > 

    <TextView 
     android:id="@+id/my_dialog_layout_message_text_view" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:layout_margin="10dp" 
     android:text="My message" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="1.0" > 

     <Button 
      android:id="@+id/my_dialog_layout_positive_bttn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:text="YES" /> 

     <Button 
      android:id="@+id/my_dialog_layout_negative_bttn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:text="NO" /> 

    </LinearLayout> 

</LinearLayout> 

을 지금 제목, 메시지와 같은 대화 데이터를 보유하는 추상 클래스를 builing 시작 , 버튼 텍스트 ... 등

package com.example.timerdialog; 

public abstract class MyDialog 
{ 
    private String title; 
    private String message; 
    private String positiveBttnText; 
    private String negativeBttnText; 
    private int time;//time in seconds 

public String getTitle() 
{ 
    return title; 
} 
public void setTitle(String title) 
{ 
    this.title = title; 
} 
public String getMessage() 
{ 
    return message; 
} 
public void setMessage(String message) 
{ 
    this.message = message; 
} 
public String getPositiveBttnText() 
{ 
    return positiveBttnText; 
} 
public void setPositiveBttnText(String positiveBttnText) 
{ 
    this.positiveBttnText = positiveBttnText; 
} 
public String getNegativeBttnText() 
{ 
    return negativeBttnText + "(" + time + ") "; 
} 
public void setNegativeBttnText(String negativeBttnText) 
{ 
    this.negativeBttnText = negativeBttnText; 
} 
public int getTime() 
{ 
    return time; 
} 
public void setTime(int time) 
{ 
    this.time = time; 
} 

}

안드로이드 활동

package com.example.timerdialog; 

import android.app.Dialog; 
import android.content.Context; 
import android.os.Handler; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class MyDialogView extends MyDialog 

{ 
private Context    context; 
private View    rootView; 
private DialogListener  dialogListener;// listener to simulate click events 
private Dialog    dialog; 

private TextView   messageTextView; 
private Button    positiveBttn; 
private Button    negativeBttn; 

private Handler    handler;// handler will be use as timer 
private Runnable   runnable; 

private int defaultTime; 

/** 
* 
* @param context 
* @param title 
* @param message 
* @param positiveBttnText 
* @param negativeBttnText 
* @param defaultTime 
*/ 
public MyDialogView(Context context,String title,String message,String positiveBttnText,String negativeBttnText,int defaultTime) 
{ 
    this.context = context; 
    this.defaultTime = defaultTime; 
    setTitle(title); 
    setMessage(message); 
    setPositiveBttnText(positiveBttnText); 
    setNegativeBttnText(negativeBttnText); 
    setTime(defaultTime); 
} 

private void buildUi() 
{ 
    if(rootView!=null) 
    { 
     messageTextView = (TextView) rootView.findViewById(R.id.my_dialog_layout_message_text_view); 
     positiveBttn = (Button) rootView.findViewById(R.id.my_dialog_layout_positive_bttn); 
     negativeBttn = (Button) rootView.findViewById(R.id.my_dialog_layout_negative_bttn); 

     messageTextView.setText(getMessage()); 
     positiveBttn.setText(getPositiveBttnText()); 
     negativeBttn.setText(getNegativeBttnText()); 

     positiveBttn.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       if(dialogListener!=null) 
       { 
        if(handler!=null) handler.removeCallbacks(runnable);//remove any queued post 
        setTime(defaultTime);//reset the default time 
        dialogListener.onPositiveClick(); 
       } 
      } 
     }); 

     negativeBttn.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       if(dialogListener!=null) 
       { 
        if(handler!=null) handler.removeCallbacks(runnable);//remove the previous post 
        setTime(defaultTime);//reset the default time 
        dialogListener.onNegativeClick(); 
       } 
      } 
     }); 

     startHandler(); 
    } 
} 

public void setOnDialogListener(DialogListener listener) 
{ 
    dialogListener = listener; 
} 
public interface DialogListener 
{ 
    public abstract void onPositiveClick(); 
    public abstract void onNegativeClick(); 
} 
/** 
* build and show dialog 
*/ 
public void show() 
{ 
    rootView = View.inflate(context, R.layout.my_dialog_layout, null); 
    dialog = new Dialog(context); 
    handler = new Handler(); 
    buildUi(); 
    dialog.setTitle(getTitle());// set title for dialog 
    dialog.setContentView(rootView);// set the content view of the dialog 
    dialog.setCancelable(false);// set the dialog to be non-cancelable 
    dialog.show(); 
} 

public void dismissDialog() 
{ 
    if(dialog!=null) dialog.dismiss(); 
} 

private void startHandler() 
{ 
    runnable = new Runnable() 
    { 

     @Override 
     public void run() 
     { 
      int time = getTime(); 
      setTime(time - 1); 
      if (time <= 0)// if time is less than or equal to zero then stop the handler 
      { 
       handler.removeCallbacks(this);//remove any queued post 
       setTime(defaultTime);// reset the default time 
       if(dialogListener != null) dialogListener.onNegativeClick();// simulate negative button click 
      } 
      else 
      { 
       buildUi();// rebuild the ui of the dialog 
      } 

     } 
    }; 
    if (handler != null) 
    { 
     handler.postDelayed(runnable, 1000);// send post after 1 second = 1000 ms 
    } 
} 

}

이 당신의 손님 대화보기를 만들려면 대화 손님 뷰 구성 요소를 업데이트 처리됩니다 클래스 뷰 홀더를 생성하고 설정하는 foget하지 않는다 대화 상자 클릭을 처리하는 대화 상자 수신기.

final MyDialogView myDialogView = new MyDialogView(MainActivity.this, "My Dialog", "This is test message", "YES", "NO",10); 
    findViewById(R.id.button1).setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      myDialogView.show(); 
     } 
    }); 


myDialogView.setOnDialogListener(new DialogListener() 
{ 

    @Override 
    public void onPositiveClick() 
    { 
     myDialogView.dismissDialog(); 
    } 

    @Override 
    public void onNegativeClick() 
    { 
     myDialogView.dismissDialog(); 
    } 
}); 

- 결과는

enter image description here

+0

많이 감사합니다! 당신은 최고입니다, 나는 그것을 지금 시도 할 것입니다! :) –

+0

대단히 감사합니다. 잘 작동합니다! –