Android实现alertDialog

Android实现alertDialog

木来 木来

实现的效果是,在一个活动中,点击按钮,弹出alertDialog,进行提示。

package com.shihsf.hello;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends BasicActivity implements View.OnClickListener{


    private static final String TAG = "MainActivity";



    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);




        Button button1=findViewById(R.id.main_button);

        button1.setOnClickListener(this);


    }

    @Override    public void onClick(View v) {

switch (v.getId()){
    case R.id.main_button:
//        Toast.makeText(MainActivity.this,"测试",Toast.LENGTH_SHORT).show();        AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
        dialog.setTitle("这是一个提醒框");
        dialog.setMessage("点我作甚,不要做无谓的抵抗!");
        dialog.setCancelable(false);
        dialog.setPositiveButton("好的", new DialogInterface.OnClickListener() {
            @Override            public void onClick(DialogInterface dialog, int which) {

            }
        });

        dialog.setNegativeButton("我不听", new DialogInterface.OnClickListener() {
            @Override            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog.show();
        break;




}


    }
}
0 条评论