2013-12-19 1 views
0

작동하지 않음 (이미지와 제목, 텍스트, 2 개 버튼) :안드로이드 - 대화가 나는이 레이아웃을 갖는 사용자 정의 대화 만드는거야

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical|center_horizontal" 
    android:background="@color/white"> 
    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="30dip" 
     android:paddingTop="10dip"> 
     <ImageView 
      android:id="@+id/dialog_title_image" 
      android:layout_alignParentLeft="true" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/info"/> 
     <TextView 
      android:id="@+id/dialog_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dip" 
      android:layout_centerInParent="true" 
      android:text="Title" 
      android:layout_toRightOf="@id/dialog_title_image" 
      android:textColor="@color/black" 
      android:textSize="30sp"/> 

    </RelativeLayout> 
    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="2dip" 
      android:background="@color/header_grep" 
      android:layout_marginTop="5dip"/> 
    <TextView 
     android:id="@+id/dialog_msg" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@color/dark_gray" 
     android:layout_marginTop="10dip" 
     android:layout_marginLeft="10dip"/> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="10dip" 
     android:gravity="bottom|center_horizontal" 
     android:paddingBottom="5dip"> 
     <Button 
      android:id="@+id/positive_button" 
      android:layout_alignParentLeft="true" 
      android:layout_width="100dip" 
      android:layout_height="wrap_content" 
      style="@style/Gradient" 
      android:text="Si"/> 
     <Button 
      android:id="@+id/negative_button" 
      android:layout_width="100dip" 
      style="@style/Gradient" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/positive_button" 
      android:text="No"/> 
    </RelativeLayout> 


</LinearLayout> 

public class CustomDialog extends Dialog { 

    Context context; 

    public CustomDialog(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
     this.context = context; 
    } 


    public CustomDialog(Context context, int theme) { 
     super(context, theme); 
     // TODO Auto-generated constructor stub 
     this.context = context; 
    } 


    public CustomDialog(Context context, boolean cancelable, 
      OnCancelListener cancelListener) { 
     super(context, cancelable, cancelListener); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     this.setContentView(R.layout.custom_dialog); 
    } 

} 

이 CustomDialog.java

입니다

 private void showDialog(String msg, String title) 

    { 
     final Dialog dialog = new CustomDialog(this); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialog.setContentView(R.layout.custom_dialog); 

    TextView title_dialog = (TextView) dialog.findViewById (R.id.dialog_title); 
    title_dialog.setText(title); 

    TextView text = (TextView) dialog.findViewById(R.id.dialog_msg); 
    text.setText(msg); 

    Button positiveButton = (Button) dialog.findViewById(R.id.positive_button); 
    Button negativeButton = (Button) dialog.findViewById(R.id.negative_button); 

    positiveButton.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View arg0) { 

      finish(); 
     } 

    }); 

    negativeButton.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View arg0) { 

      dialog.dismiss(); 
     } 

    }); 

    dialog.show(); 

} 

내가 앱을 실행하고 대화 상자가 표시되면 제목과 텍스트가 그 것을 표시하는 방법입니다. d efined xml 파일 (그래서 '제목'과 아무것도 대화 본문에 대한) 그리고 만약 내가 버튼을 클릭 아무것도 일어나지 않습니다. 아무도 도와 줄 수 있습니까? 미리 감사드립니다.

+2

'최종 대화 상자 대화 상자 = 새 대화 상자 (YourActivity.this);' –

+1

시도해보십시오! 이제 작동합니다! 내가 뭘 잘못했는지 설명해 줄 수 있니? – SegFault

+0

@SegFault 작동하도록 변경하려면 어떻게해야합니까? – keshav

답변

1

대화 상자에서 응용 프로그램 컨텍스트를 전달해야합니다. 컨텍스트로 이것을 전달하고 버튼 onclick() 이벤트에서 customdialog를 호출하면이 버튼이 onclicklistner() 객체의 버튼으로 참조되며 잘못된 것입니다. 그래서 당신은 당신의 활동이나 응용 컨텍스트를 통과해야합니다.

final Dialog dialog = new CustomDialog(yourappliactioncontext); 
0

하면 밖으로 시도 :

final Dialog dialog = new Dialog(YourActivity.this);

대신

final Dialog dialog = new Dialog(context);

이 코드를 재사용하기 위하여려고하는 경우에, 재사용 가능한 구성 요소로 또는 생성하는 메커니즘으로 대화 상자를 여러 곳에서 작성하고 기본 활동 클래스를 작성한 다음이 메소드를 필요에 따라 하위 클래스 활동에 사용하십시오.