2016-08-10 8 views
0

Интересно, как нажать на кнопку внутри AlertDialog в Android, и это мой кодAndroid - Нажмите кнопку Inside 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

У меня есть AlertDialog с .setView выше – 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 Извините, я вас не понял? В любом случае вы можете ссылаться на ссылку, содержащую все типы диалоговых окон в android –

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 Code

// 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(); 
} 
}); 
Смежные вопросы