2011-01-02 3 views
1

일부 유효성 검사 (아래)를 수행하는 대화 상자가 있습니다. 문제는 토스트가 표시된 후에 대화가 닫히고, 내가 전화를 끊지 않고서입니다. 건배를 보여주고 오류를 수정하기 위해 대화 상자를 열어 두어야합니다.Android : 닫기를 호출하지 않고 대화를 닫습니다.

final EditText txtName = new EditText(this); 
AlertDialog.Builder dlgAdd = new AlertDialog.Builder(this) 
    .setTitle(R.string.create_category) 
    .setMessage(R.string.name) 
    .setView(txtName) 
    .setPositiveButton(R.string.ok, new OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      String newCatName = txtName.getText().toString().trim(); // Converts the value of getText to a string. 
      if (newCatName != null && newCatName .length() ==0) 
      { 
       Toast.makeText(ManageCategories.this, R.string.err_name_required, 3500).show(); 

      } else { 
       try { 
        boolean alreadyExists = mDatabaseAdapter.getCategoryIDs(newCatName).length > 0;// ids of cats with this name 
        if(alreadyExists) { 
         Toast.makeText(ManageCategories.this, R.string.categoryAlreadyExists, 3500).show(); 
        } else { 
         mDatabaseAdapter.addCategory(newCatName); 
        } 
       }catch(Exception ex){ 
        Toast.makeText(ManageCategories.this, R.string.error+':'+ ex.getLocalizedMessage(), 3500).show(); 
      } 
      } 
     } 
    }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
      } 
}); 
dlgAdd.show(); 
+1

이 해결 된 방법을 기억 하는가? 수락 된 답변은 도움이되지 않습니다. 정확한 페이지에서 읽을 수 있습니다. * 사용자가 AlertDialog.Builder로 생성 된 액션 버튼을 터치하면, 시스템이 대화 상자를 닫습니다. *에 따르면, onPositiveClick을 보여주는 대화 상자를 유지하십시오. – natario

답변

1

내 생각 엔 기능

을이 onCreateDialog 사용하여 여기 http://developer.android.com/guide/topics/ui/dialogs.html 안드로이드 문서에서 언급 한대로 작성하고 대화 상자가 표시되지 않는 것입니다 워드 프로세서에서 언급 할 그것은 여전히 ​​작동하지 않는 경우 알려 주시기 바랍니다 .

+0

수정하십시오. 작동했지만 재사용이 가능한 대화 클래스를 만들 필요가있었습니다. – Echilon

+0

AlertDialog.Builder create() 위 코드에 같은 효과로 http://developer.android.com/reference/android/app/AlertDialog.Builder.html#create()를 사용할 수 있습니다. 재사용이 가능한 대화 상자 클래스를 만들지 않아도됩니다. – the100rabh

+0

그것은 나를 위해 작동하지 않습니다 : [this snippet] (http://developer.android.com/guide/topics/ui/dialogs.html#DialogFragment)와 같이, 나는 DialogFragment를 확장하고 대화 상자를 반환합니다. onCreateDialog()'; 그래도 사용자가 버튼을 탭하면 오류 확인을 할 수 없습니다. 대화 상자가 자동으로 닫히고 계속 유지할 수 없습니다. – natario

1

나는 당신이 무엇을 달성하려고하는 것은 AlertDialog.bilder 으로 대신에 당신이 대화의

  1. 개체를 만들 수 불가능하다 생각합니다.
  2. 대화 상자의 레이아웃을 설정하십시오.
  3. 적절한 수신기를 설정하십시오.

예.

dialog_view.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" android:orientation="vertical"> 

<EditText 
      android:layout_height="wrap_content" 
      android:id="@+id/EditText01" android:layout_width="300dip" android:ellipsize="none"/> 

<LinearLayout 
     android:id="@+id/LinearLayout01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <Button 
       android:id="@+id/Button01" 
       android:layout_height="wrap_content" 
       android:text="Yes" 
       android:layout_width="100dip"/> 

     <Button 
       android:id="@+id/Button02" 
       android:layout_height="wrap_content" 
       android:text="No" 
       android:layout_width="100dip"/> 
</LinearLayout> 

    </LinearLayout> 

Help.java

public class Help extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    d = new Dialog(Help.this, 
      android.R.style.Theme_InputMethod); 

    createMyDialog(); 
} 
    private Dialog d; 
private void createMyDialog() { 
    d.setContentView(R.layout.dialog_view); 
    Button b1 = (Button)findViewById(R.id.Button01); 
    Button b2 = (Button)findViewById(R.id.Button02); 
    EditText t = (EditText) findViewById(R.id.EditText01); 
    OnTouchListener listner1 = null; 
    OnTouchListener listner2 = null; 
    b1.setOnTouchListener(listner1); 
    b2.setOnTouchListener(listner2); 
    listner1 = new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      return false; 
     } 
    }; 
    listner2 = new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      return false; 
     } 
    }; 
      d.show(); 
} 

}

관련 문제