Android - GenyMotion 中的警报对话框问题

Android - Alert Dialog issue in GenyMotion

我是 android 编程的新手,我想制作一个警报对话框 project.my xml 文件没问题,但是当我在 genymotion 中打开应用程序时,它不显示警报dialog.this 是我的 java 代码:

package karimi.afshin.alertdialog;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void open(View view){
    AlertDialog.Builder alertDialogBuilder=new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("Are you sure,You wanted to make decision");

    alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            Toast.makeText(MainActivity.this,"You clicked yes button",Toast.LENGTH_LONG).show();
        }
    });
    alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });
    AlertDialog alertDialog=alertDialogBuilder.create();
    alertDialog.show();
}

}

这是我的 xml 文件:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Alert Dialog"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="32dp"
    android:id="@+id/textView1"
    android:textSize="25sp"
    android:textColor="@color/background_floating_material_dark" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Tutorialspoint"
    android:id="@+id/textView2"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="25dp"
    android:textColor="#3db226"
    android:textSize="40sp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:src="@drawable/abc"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="46dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ALERT DIALOG"
    android:id="@+id/button"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp" />

我想要它像这个图片但是它没有显示对话框!

谢谢:)

试试这个, 首先你必须点击打开对话框的按钮,

onCreate 方法中的类似内容

Button btn
btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager fm = getActivity().getSupportFragmentManager();
                TestDialog dialog= new TestDialog ();
                dialog.show(fm,"fragment name");
}

现在您必须像这样创建新的 class 名称 TestDialogextends

public class TestDialog extends DialogFragment  {


public TestDialog () {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout."example.xml", container, false);

//declare your views here like buttons or edit text  
    return rootView;}
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = new Dialog(getActivity(), getTheme());
    final View view = getActivity().getLayoutInflater().inflate(
            R.layout.actionoption, null);
    dialog.setTitle(getResources().getString(R.string.action_settings));
    dialog.setCanceledOnTouchOutside(false);
    return dialog;

}
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    getDialog().setTitle("Actions Option");


}}

希望这对任何问题有帮助,请在评论中告诉我

您无需更改代码中的任何内容。只需在按钮布局中添加 android:onClick="open" 即可。像这样:

...
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ALERT DIALOG"
        android:onClick="open"
        android:id="@+id/button"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp" />
...

希望对您有所帮助!