Java Android-单击alertDialog内的按钮

Java Android-单击alertDialog内的按钮,java,android,Java,Android,我想知道如何在Android中单击AlertDialog中的按钮,这是我的代码 活动\u float\u info.xml <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button" /> 我把布局图放大了。。 我认为主要的问题在于 button =

我想知道如何在Android中单击
AlertDialog
中的按钮,这是我的代码

活动\u float\u info.xml

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button" />
我把布局图放大了。。 我认为主要的问题在于

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

尝试此操作“为创建布局”对话框


请参阅:

尝试此对话框创建布局


请参阅:

这里是一个使用
警报对话框的黑客:

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();
以及对话框的布局(除按钮外,对话框中没有任何内容):



当你点击按钮时,你应该可以看到祝酒词。希望这能有所帮助。

这里有一个使用
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();
以及对话框的布局(除按钮外,对话框中没有任何内容):



当你点击按钮时,你应该可以看到祝酒词。希望这有帮助。

对话框就像一个弹出窗口,向用户显示一些选项(如接受/拒绝)

使用类android.app.Dialog创建对话框

使用dialog.xml文件创建自定义对话框布局

例如: res/layout/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>

对话框就像一个弹出窗口,向用户显示一些选项(如接受/拒绝)

使用类android.app.Dialog创建对话框

使用dialog.xml文件创建自定义对话框布局

例如: res/layout/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>

创建自定义对话框。我在上面有.setView的AlertDialog创建自定义对话框。我在上面有.setView的AlertDialog使用AlertDialog怎么样?@MochamadLuckyPradana抱歉,我没有理解你的意思?无论如何,你可以参考链接,它包含Android中所有类型的对话框。如何使用AlertDialog?@MochamadLuckyPradana抱歉,我没有收到你的消息?无论如何,你可以引用这个链接,它包含android中所有类型的对话框
<?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>
// 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();
}
});