2012-08-28 24 views
7

私は独学で初心者です。ありがとう!カスタムアラートダイアログ内のOnClickListener Android

Eclipseでは、独自のxmlファイル(custom_dialog)を使用してカスタムアラートダイアログを作成しました。これは「usernamealert」と呼ばれています。

ユーザーがまだユーザー名を入力していない場合(つまり、username.length == 0)、アラートをポップアップします。

このレイアウト内には、textView(「あなたの名前は何ですか?」)、editText、およびボタン(「usernameButton」)があります。

ボタンのonclicklistenerを入れる前に、すべてが機能しました。これは私の(関連する)Javaでした:

LayoutInflater inflater = getLayoutInflater(); 
View dialoglayout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)  getCurrentFocus()); 
AlertDialog.Builder usernamebuilder = new AlertDialog.Builder(this); 
usernamebuilder.setView(dialoglayout); 

AlertDialog usernamealert = usernamebuilder.create(); 

私はonclicklistenerを置くと、それは壊れました!どこに置いたらいいですか?コードの後

(以下は私が...私のOnCreateイベントですべて試していたものです)

LayoutInflater inflater = getLayoutInflater(); 
View dialoglayout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)getCurrentFocus()); 
AlertDialog.Builder usernamebuilder = new AlertDialog.Builder(this); 
usernamebuilder.setView(dialoglayout); 


Button usernameButton = (Button) usernamealert.findViewById(R.id.usernameButton); 
usernameButton.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View v) { 
//store username in sharedprefs 
usernamealert.dismiss(); 



} 
}); 

私は言った:

再び
if (username.length() == 0) { 
      usernamealert.show(); 
     } 

、私はいじり開始する前に、それが働きましたボタン!!

+0

AFTEあるhireteachernegotiate.xml

AlertDialog.Builder builder = new AlertDialog.Builder(this); // Get the layout inflater LayoutInflater inflater =getLayoutInflater(); View myview = inflater.inflate(R.layout.dialoghireteachernegotiate, null); // Inflate and set the layout for the dialog // Pass null as the parent view because its going in the dialog layout builder.setView(myview); Button addS = (Button) myview.findViewById (R.id.bAddS); addS.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //do some stuff } }); Button minusS = (Button) myview.findViewById (R.id.bMinusS); addS.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //do other stuff } }); // Add action buttons builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); builder.show(); 

のレイアウトになっていHireteachernegotiate.class、 r時間の検索と最後に質問を投稿すると、私はちょうど2分後にそれを理解しました! ボタンを見つけた方法を変更するだけでした。 Button usernameButton =(Button)usernamealert.findViewById(R.id.usernameButton); それがあったはずです: Button usernameButton =(Button)dialoglayout.findViewById(R.id.usernameButton); // dialoglayoutはあなたがViewと呼んでいるものです – delfina

+0

ようこそStackOverflow!答えとしてあなたの解決策を掲示し、あなたがそれを許した後に正しい答えとしてそれを受け入れることができますか?将来の人々が同じ問題を抱えていれば、それを簡単に見つけることができます。 – FoamyGuy

+0

あなたの答えが私を助けました。あなたの答えをより読みやすくしてください。私の答えを以下に追加します。 – mcr619619

答えて

2

これを試してください。

usernamebuilder.setCancelable(false) 
usernamebuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      //do what you want. 
     } 
    }); 

これが機能するか、何らかの形で役立つかどうかを確認してください。それはホストのXMLで検索するでしょう、その唯一の「findViewById」ならば、それは

LayoutInflater inflater =getLayoutInflater(); 
       View myview = inflater.inflate(R.layout.dialoghireteachernegotiate, null); 
       // Inflate and set the layout for the dialog 
       // Pass null as the parent view because its going in the dialog layout 
       builder.setView(myview); 
       Button addS = (Button) myview.findViewById (R.id.bAddS); 

でなければなりません。これは私のクラスの一部のコード検索はボタン意志をある場所を指定するために必要とされる

+1

ImageViewにカスタムonClickListenerを追加すると、このダイアログが表示されません。 – 4ndro1d

+0

@ 4ndro1d "dialog.dismiss();"を使用してください。 setOnClickListener()内にあるonClickメソッドの内部。 – AJW

7

、これは、ダイアログレイアウト

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <Button 
     android:id="@+id/bAddS" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 


    <Button 
     android:id="@+id/bMinusS" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

</LinearLayout> 
+0

ダイアログレイアウト名は "dialoghireteachernegotiate.xml"です。 – mcr619619

関連する問題