Android - GenyMotion中的警报对话问题

时间:2016-10-07 20:46:22

标签: android genymotion

我是android编程的新手,我想制作一个警告对话框project.my xml文件没问题但是当我在genymotion中打开应用程序时它没有显示警告对话框。这是我的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" />

我希望它像这张图片,但它没有显示对话框!

enter image description here

感谢:)

2 个答案:

答案 0 :(得分:0)

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

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");
}

现在你必须像这样创建新的类名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");


}}

希望对任何问题的帮助在评论中告诉我

答案 1 :(得分:0)

您无需更改代码中的任何内容。只需在Button布局中添加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" />
...

希望这有帮助!