2011-01-02 23 views
1

私はいくつかのバリデーション(下記)を行うダイアログを持っています。問題は、Toastが表示された後、私が却下を呼ぶことなくダイアログが閉じられることです。トーストを表示し、エラーを修正するためにダイアログを開いたままにする必要があります。アンドロイド:ダイアログを閉じずに閉じるダイアログ

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 Androidのドキュメントで説明したように、あなたが作成し、ダイアログボックスを表示されていないということであるドキュメントで述べたように行うと、それはまだ動作しない場合はお知らせください。 。

+0

正しい。作業はしましたが、再利用可能なダイアログクラスを作成する必要がありました。 – Echilon

+0

AlertDialog.Builder create()上に配置したコードでshow()の代わりに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

私は何を達成しようとしていることは、あなたがダイアログの

  1. オブジェクトを作ることができますAlertDialog.bilder ではなく、可能性がないと思います。
  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(); 
} 

}

関連する問題