2012-02-25 5 views
1

사용자 정의 가능한 대화 상자를 표시하려고합니다. 이 대화 상자에는 3 개의 edittext와 하나의 timepicker가 있습니다. 화면의 버튼을 누르면이 대화 상자가 표시됩니다. Google 자습서를보고 코드를 작성하려고합니다. 그러나 인플레이터의 루트 레이아웃을 버튼을 누르는 레이아웃으로 사용하면 버튼 아래에 대화 상자가 추가됩니다. dialog_xml에서 루트 레이아웃을 사용했을 때 버튼이 작동하지 않습니다. 해당 코드의 일부가 아래에 있습니다. 당신은 어떤 생각을 가지고 있습니까? 나는 이것을 올바른 방법으로 어떻게 할 수 있습니까?사용자 정의 대화 상자 Android

Button ekleButton; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.haphatirlatici); 

     // After creating the activity setting other things for running 
     ekleButton = (Button) findViewById(R.id.EkleButton); 
     ekleButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      // Dialog icin yerlesimler 

     AlertDialog.Builder builder; 
     AlertDialog alertDialog; 

     Context mContext = getApplicationContext(); 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); 
     View layout = inflater.inflate(R.layout.ekle_dialog, 
             (ViewGroup) findViewById(R.id.Ekle_Layout)); 

     builder = new AlertDialog.Builder(mContext); 
     builder.setView(layout); 
     alertDialog = builder.create(); 

     } 
    }); 

이 코드를 편집하고 두 개의 버튼을 추가하십시오. 표시되는 활동에 대한 경고 대화 상자의 결과를 얻고 싶습니다. 필자가 작성한 코드는 아래에 있습니다.

공용 클래스 HapHatirlatici는 활동을 {

Button ekleButton; 
boolean eklendiMi; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.haphatirlatici); 

    // After creating the activity setting other things for running 
    ekleButton = (Button) findViewById(R.id.EkleButton); 
    eklendiMi = false; 
    ekleButton.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     View layout = getLayoutInflater().inflate(R.layout.ekle_dialog, null);        
     AlertDialog.Builder builder = new AlertDialog.Builder(HapHatirlatici.this); 
     builder.setPositiveButton(R.string.ekle,new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       eklendiMi = true; 
      } 
     }); 
     builder.setNegativeButton(R.string.vazgec, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       eklendiMi = false; 
      } 
     }); 
     builder.setView(layout); 
     AlertDialog alertDialog = builder.create(); 
     alertDialog.setTitle("Ilac Ekleme"); 
     alertDialog.show(); 


     } 
    }); 
} 
public boolean databaseEkle() 
{ 
    boolean sonuc = false; 
    return sonuc; 
} 

} 

답변

1

당신은 실제로 당신이 단지 내장 된 대화 상자를 표시합니다 alertDialog.show();를 잊어 버렸 확장합니다.

또한, 코드를 단순화 할 수있다 :

물론
Button ekleButton; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.haphatirlatici); 

    // After creating the activity setting other things for running 
    ekleButton = (Button) findViewById(R.id.EkleButton); 
    ekleButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      View layout = getLayoutInflater().inflate(R.layout.ekle_dialog, null);        
      AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityClass.this); 
      builder.setView(layout); 
      AlertDialog alertDialog = builder.create(); 

      // this is what you forgot: 
      alertDialog.show(); 
     } 
    }); 
} 

, 실제 활동 이름으로 YourActivityClass을 교체합니다.

+0

고마워요. 이거 작동 중입니다 .. –

+0

제발 편집 해 주시겠습니까? –

관련 문제