美文网首页
LogCat调式程序,Toast动态显示信息,AlertDial

LogCat调式程序,Toast动态显示信息,AlertDial

作者: 晨曦诗雨 | 来源:发表于2018-12-26 09:03 被阅读0次
image.png
日志
image.png

Toast

image.png
添加带图片的弹框
 Toast toast=Toast.makeText(OptionMenue.this,"添加图片",Toast.LENGTH_SHORT);
             LinearLayout linearLayout=(LinearLayout)toast.getView();
             ImageView imageView=new ImageView(this);
             imageView.setImageResource(R.drawable.select);
             linearLayout.addView(imageView);
             toast.show();
完全自定义的toast
 private  void  showToast(){
        LayoutInflater inflater=LayoutInflater.from(this);
        View view=inflater.inflate(R.layout.toast_view,null);
        Toast toast=new Toast(this);
        toast.setView(view);
         toast.show();
    }

AlertDialog对话框

image.png
常见的对话框
image.png
常见的方法
image.png

实现常见的几种对话框(java代码实现功能)

public class Dialog extends AppCompatActivity{
    private Button button;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;
    private  String[] sing_list={"男","女","男程序员","女程序员"};
    private  String[] multi_list={"篮球","足球","男程序员","女程序员"};
    private  String[] item_list={"项目经理","设计师","策划","程序员","测试"};
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        button=(Button)findViewById(R.id.sure);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                          showDialog1();
            }
        });
        button2=(Button)findViewById(R.id.btn2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog2();
            }
        });
        button3=(Button)findViewById(R.id.btn3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog3();
            }
        });
        button4=(Button)findViewById(R.id.btn4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog4();
            }
        });
        button5=(Button)findViewById(R.id.btn5);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog5();
            }
        });
    }
    private  void showDialog1(){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("确定对话框");
        builder.setIcon(R.drawable.ic_launcher);
        builder.setMessage("确认对话框提示啊内容");
        builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(Dialog.this,"点击啦确定",Toast.LENGTH_SHORT).show();

            }
        });
        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(Dialog.this,"点击啦取消",Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog dialog=builder.create();
        dialog.show();

    }
    private  void showDialog2(){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("单选按钮对话框");
        builder.setIcon(R.drawable.ic_launcher);
        //builder.setMessage("确认对话框提示啊内容");
        builder.setSingleChoiceItems(sing_list,0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String str=sing_list[which];
                Toast.makeText(Dialog.this,str,Toast.LENGTH_SHORT).show();
            }
        });
        builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(Dialog.this,"点击啦确定",Toast.LENGTH_SHORT).show();

            }
        });
        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(Dialog.this,"点击啦取消",Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog dialog=builder.create();
        dialog.show();

    }
    private  void showDialog3(){
        final AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("单选按钮对话框");
        builder.setIcon(R.drawable.ic_launcher);
        builder.setMultiChoiceItems(multi_list, null, new DialogInterface.OnMultiChoiceClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which, boolean isChecked) {
              if (isChecked){
                  Toast.makeText(Dialog.this,"我喜欢上啦"+multi_list[which],Toast.LENGTH_SHORT).show();
              }else {
                  Toast.makeText(Dialog.this,"我不喜欢上啦"+multi_list[which],Toast.LENGTH_SHORT).show();

              }

          }
      });
        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Toast.makeText(Dialog.this,"点击啦取消",Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog dialog=builder.create();
        dialog.show();

    }
    private  void showDialog4(){
        final AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("显示列表对话框");
        builder.setIcon(R.drawable.ic_launcher);
        builder.setItems(item_list, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(Dialog.this,"我认识啦"+item_list[which],Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Toast.makeText(Dialog.this,"点击啦取消",Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog dialog=builder.create();
        dialog.show();

    }
    private  void showDialog5(){
        LayoutInflater layoutInflater=LayoutInflater.from(this);
        View view=layoutInflater.inflate(R.layout.dialog_activity,null);
        final AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("显示列表对话框");
        builder.setIcon(R.drawable.ic_launcher);
       builder.setView(view);
        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Toast.makeText(Dialog.this,"点击啦取消",Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog dialog=builder.create();
        dialog.show();

    }
xml布局来设置按钮点击弹出对话框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/sure"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="确认对话框"/>
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="单选对话框"/>
    <Button
        android:id="@+id/btn3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="多选对话框"/>
    <Button
        android:id="@+id/btn4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="列表对话框"/>
    <Button
        android:id="@+id/btn5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="自定义对话框"/>

</LinearLayout>
设置自定义的对话框xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <EditText
            android:layout_width="match_parent"
            android:hint="请输入内容"
            android:layout_height="wrap_content"
            android:id="@+id/endit"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="提交"
            android:layout_marginLeft="10dp"
            android:id="@+id/button"/>
    </LinearLayout>
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/info"/>
</LinearLayout>

相关文章

网友评论

      本文标题:LogCat调式程序,Toast动态显示信息,AlertDial

      本文链接:https://www.haomeiwen.com/subject/vwgakqtx.html