2016-08-10 11 views
0

内側のボタンをクリックしますアンドロイド - 私は、Androidで<code>AlertDialog</code>の内側のボタンをクリックする方法を疑問に思うと、これは私のコードであるalertDialog

activity_float_info.xml

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

MainActivity.java

QR_ = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_float_info, null); 
     MAIN_QR_SCAN = (LinearLayout) findViewById(R.id.MAIN_QR_SCAN); 

     MAIN_QR_SCAN.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       new AlertDialog.Builder(MainActivity.this) 
         .setCancelable(Boolean.TRUE) 
         .setNegativeButton(getString(R.string.CANCEL), new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialogInterface, int i) { 
           dialogInterface.dismiss(); 
          } 
         }) 
         .setView(R.layout.activity_float_info) 
         .show(); 

       button = (Button)QR_.findViewById(R.id.button); 
       button.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         button.setText("TEst"); 
        } 
       }); 
      } 
     }); 

私はレイアウトを膨張させる。 私は主な問題があると思う。

button = (Button)QR_.findViewById(R.id.button); 
+1

カスタムダイアログを作成します。 –

+0

私は上記の.setViewを持つAlertDialogを持っています – Ampersanda

答えて

1

このダイアログボックスのレイアウトを作成してみてください

<TextView android:id="@+id/dialogtitle" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:textColor="@android:color/black" 
    android:text="Please enter the email address you used for the account" 
    /> 
<EditText 
    android:id="@+id/emailedittext" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp" 
    android:ems="10" 
    android:padding="5dp" 
    android:cursorVisible="true" 
    android:singleLine="true" 
    android:background="@android:color/white" 
    android:textColor="@android:color/black" 
    android:hint="Enter Mail id" 
    android:textSize="20dp" > 
    <requestFocus /> 
</EditText> 

<LinearLayout android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="2" 
    android:orientation="horizontal"> 

    <Button 
     android:id="@+id/cancelbtn" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="CANCEL"/> 

     <Button 
     android:id="@+id/okbtn" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="Ok"/> 
</LinearLayout> 

はその後、このレイアウトを使用して、ダイアログボックスを作成し、ハンドルのボタンは

final Dialog dialog = new Dialog(MainActivity.this); 
     // Include dialog.xml file 
     dialog.setContentView(R.layout.forgotpassword); 
     // Set dialog title 
     dialog.setTitle("ALERT!!"); 
     // set values for custom dialog components - text, image and button 
     Button okbtn = (Button) dialog.findViewById(R.id.okbtn); 
     Button cancelbtn = (Button) dialog.findViewById(R.id.cancelbtn); 
     final EditText emailedittext = (EditText) dialog.findViewById(R.id.emailedittext); 
     dialog.show(); 
     dialog.getWindow().setSoftInputMode(
       WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
     // if decline button is clicked, close the custom dialog 
     cancelbtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // Close dialog 
       dialog.dismiss(); 
      } 
     }); 
     okbtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       String email=emailedittext.getText().toString(); 
       //do something more here 
      } 
     }); 

を参照してくださいクリック:http://coderzpassion.com/android-show-alertdialog/

+0

AlertDialogの使い方はどうですか? – Ampersanda

+0

@MochamadLuckyPradana申し訳ありませんが、あなたを取得していない?とにかくアンドロイドのすべてのタイプのダイアログボックスを含むリンクを参照することができます –

0

ここにAlertDialogを使用するハック:

public class CustomDialog extends AlertDialog { 

    protected CustomDialog(Context context) { 
     super(context); 
    } 
} 

そして、あなたの活動に:

 CustomDialog dialog = new CustomDialog(this); 
     View view = getLayoutInflater().inflate(R.layout.custom_dialog_layout,null); 
     dialog.setView(view); 
     Button button = (Button)view.findViewById(R.id.custom_button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(YourActivity.this,"Your message", Toast.LENGTH_LONG).show(); 
      } 
     }); 
     dialog.show(); 

と(ボタンがなければ、それは何もありません)ダイアログのレイアウト:

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

    <Button 
     android:id="@+id/custom_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="New Button" /> 

</RelativeLayout> 

とをあなたはボタンをクリックするとトーストを見ることができるはずです。お役に立てれば。

0

ダイアログは、ユーザーにいくつかのオプションを表示するポップアップウィンドウのようなものです(受け入れ/辞退などのオプション)。

android.app.Dialogクラスを使用してダイアログを作成します。

dialog.xmlファイルを使用してカスタムダイアログレイアウトを作成します。

例: 解像度/レイアウト/ dialog.xml

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

<ImageView 
    android:id="@+id/imageDialog" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="6dp" /> 

<TextView 
    android:id="@+id/textDialog" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#FFF" 
    android:layout_toRightOf="@+id/imageDialog"/> 

<Button 
    android:id="@+id/declineButton" 
    android:layout_width="100px" 
    android:layout_height="wrap_content" 
    android:text=" Submit " 
    android:layout_marginTop="5dp" 
    android:layout_marginRight="5dp" 
    android:layout_below="@+id/textDialog" 
    android:layout_toRightOf="@+id/imageDialog" 
    /> 

</RelativeLayout> 

Javaコード

// Create custom dialog object 
final Dialog dialog = new Dialog(CustomDialog.this); 
// Include dialog.xml file 
dialog.setContentView(R.layout.dialog); 
// Set dialog title 
dialog.setTitle("Custom Dialog"); 

// set values for custom dialog components - text, image and button 
TextView text = (TextView) dialog.findViewById(R.id.textDialog); 
text.setText("Custom dialog Android example."); 
ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog); 
image.setImageResource(R.drawable.image0); 

dialog.show(); 

Button declineButton = (Button) dialog.findViewById(R.id.declineButton); 
// if decline button is clicked, close the custom dialog 
declineButton.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
// Close dialog 
dialog.dismiss(); 
} 
}); 
関連する問題