2016-08-16 17 views
0

ボタンをクリックするとYes Noダイアログが表示され、Yesを押した場合にメッセージを送信しようとしています。2回目のクリック後にのみ表示される警告ダイアログ

私の問題は、ボタンの2回目のクリック後にのみ警告が表示されることです。

ボタンXML

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/clrTotalButton" 

    android:clickable="false" 
    android:enabled="true" 
    android:onClick="dialogevent" 
    android:layout_alignParentStart="false" 
    android:layout_alignParentEnd="false" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="25dp" 
    android:focusable="true" 
    android:background="@drawable/clrtotal_btn"/

ボタン定義:

public static View clrTotalBtn; 

と警告ウィンドウとコマンド:

public void dialogevent(View view){ 

    clrTotalBtn = (Button) findViewById(R.id.clrTotalButton); 
    clrTotalBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      AlertDialog.Builder altdial = new AlertDialog.Builder(DeviceControlActivity.this); 
      altdial.setMessage("ARE YOU SURE TO CLEAR TOTAL?").setCancelable(false) 
        .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          writeClrTotal(); 
         } 
        }) 
        .setNegativeButton("No",new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.cancel(); 
         } 
        }); 

      AlertDialog alert = altdial.create(); 
      alert.setTitle("Warning!"); 
      alert.show(); 
     } 
    }); 
} 

多くの感謝!

+1

なぜあなたは 'android:clickable =" false "'を追加しましたか? –

+0

それは古い定義だ、私はそれを逃した..ありがとう –

答えて

0

ボタンをクリックする前にOnClickListenerを設定する必要があります。現在、ボタンをクリックするとリスナーが登録されるため、アクションが発生する前にリスナーを2回クリックする必要があります。あなたがコードでそれをやりたければ、例えば登録する必要があります。 onCreate中にあなたは、XMLの道に行きたい場合は、あなただけのリスナーを登録するコードを削除し、そこだけ実際のリスナーの内容を残すことができます。

public void dialogevent(View view){ 

    AlertDialog.Builder altdial = new AlertDialog.Builder(DeviceControlActivity.this); 
    altdial.setMessage("ARE YOU SURE TO CLEAR TOTAL?").setCancelable(false) 
        .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          writeClrTotal(); 
         } 
        }) 
        .setNegativeButton("No",new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.cancel(); 
         } 
        }); 

      AlertDialog alert = altdial.create(); 
      alert.setTitle("Warning!"); 
      alert.show(); 
} 

編集: そしてもちろん、あなたのボタンXMLをクリーンアップ:

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/clrTotalButton" 
    android:onClick="dialogevent" 
    android:layout_alignParentStart="false" 
    android:layout_alignParentEnd="false" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="25dp" 
    android:background="@drawable/clrtotal_btn" /> 
+0

説明をありがとう! –

+0

あなたの問題を解決した場合はこれを答えにしてください; –

0

これは、OnClickを2回定義するためです。一度あなたのXMLに入れられ、それがクリックされると、オーバーレイを表示するためにクリックする必要がある新しいOnClickListenerを定義します。このコード 用途:あなたが最初のクリックと第二表示するダイアログで再度onClickListenerを定義するので

public void dialogevent(View view){ 
    AlertDialog.Builder altdial = new AlertDialog.Builder(DeviceControlActivity.this); 
     altdial.setMessage("ARE YOU SURE TO CLEAR TOTAL?").setCancelable(false) 
       .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         writeClrTotal(); 
        } 
       }) 
       .setNegativeButton("No",new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 
        } 
       }); 

     AlertDialog alert = altdial.create(); 
     alert.setTitle("Warning!"); 
     alert.show(); 
} 

public void dialogevent(View view){ 
     AlertDialog.Builder altdial = new AlertDialog.Builder(DeviceControlActivity.this); 
     altdial.setMessage("ARE YOU SURE TO CLEAR TOTAL?").setCancelable(false) 
       .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         writeClrTotal(); 
        } 
       }) 
       .setNegativeButton("No",new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 
        } 
       }); 

     AlertDialog alert = altdial.create(); 
     alert.setTitle("Warning!"); 
     alert.show(); 
    } 
1

が、それはこのようにする必要があり、この

clrTotalBtn = (Button) findViewById(R.id.clrTotalButton); 
clrTotalBtn.setOnClickListener(new View.OnClickListener() { 

を削除します。 xmlでclickイベントを定義すると、クリックリスナーを設定する必要があります。

0

dialogevent(View)メソッドからボタンonClickListenerを削除します。

public void dialogevent(View view){ 

     AlertDialog.Builder altdial = new AlertDialog.Builder(DeviceControlActivity.this); 
     altdial.setMessage("ARE YOU SURE TO CLEAR TOTAL?").setCancelable(false) 
       .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         writeClrTotal(); 
        } 
       }) 
       .setNegativeButton("No",new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 
        } 
       }); 

     AlertDialog alert = altdial.create(); 
     alert.setTitle("Warning!"); 
     alert.show(); 
    } 
});} 
+0

ありがとうございました! –

0

あなたはalrady XML上のonClickコードを適用し、あなたのコードでは、このコード

XMLボタンのコード

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/clrTotalButton" 
    android:onClick="dialogevent" 
    android:layout_alignParentStart="false" 
    android:layout_alignParentEnd="false" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="25dp" 
    android:focusable="true" 
    android:background="@drawable/clrtotal_btn"/

Javaコード

public void dialogevent(View view){ 


      AlertDialog.Builder altdial = new AlertDialog.Builder(DeviceControlActivity.this); 
      altdial.setMessage("ARE YOU SURE TO CLEAR TOTAL?").setCancelable(false) 
        .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          writeClrTotal(); 
         } 
        }) 
        .setNegativeButton("No",new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.cancel(); 
         } 
        }); 

      AlertDialog alert = altdial.create(); 
      alert.setTitle("Warning!"); 
      alert.show(); 

} 

を使用して行うことができます。

0

私は、XMLファイルから

android:focusable="true" 

を削除すると、あなたの問題を解決すると思います。

関連する問題