第一天作业

第四题

package com.example.app2;

import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class EP_3 extends AppCompatActivity {
    private ListView lv;
    private List<String> al=new ArrayList();
    private TextView tv;
    private static final String TAG = "123321";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ep_3);
        tv=findViewById(R.id.tv);

        lv=findViewById(R.id.ep3_list);
        for (int i = 0; i < 10; i++) {
            al.add(i+"i"+i+"i"+i+"i"+i+"i"+i+"i"+i+"i"+i+"i"+i+"i"+i+"i");
        }
        final ArrayAdapter aaa = new ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item,al);

        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(EP_3.this);
                builder.setMessage(al.get(position)).setNegativeButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        al.remove(position);
                        aaa.notifyDataSetChanged();
                    }
                }).create().show();
                return false;
            }
        });
        lv.setAdapter(aaa);
    }

    public void click(View view) {
        PopupMenu ppp = new PopupMenu(this, tv);
        Menu menu = ppp.getMenu();
        menu.add("蓝色");
        menu.add("红色");
        menu.add("绿色");
        ppp.show();
        ppp.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch( item.toString()){
                    case "蓝色":
                        tv.setTextColor(Color.BLUE);
                        break;
                    case "红色":
                        tv.setTextColor(Color.RED);
                        break;
                    case "绿色":
                        tv.setTextColor(Color.GREEN);
                        break;
                }
                return false;
            }
        });
    }
}
第三题的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".EP_3">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击弹出上下文菜单"
        android:onClick="click"
        android:id="@+id/tv"
        />
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:id="@+id/ep3_list"
        android:layout_below="@id/tv"
        >

    </ListView>
</RelativeLayout>

第三题代码

package com.example.app2;

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

public class EP_2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ep_2);
    }

    public void click(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View inflate = LayoutInflater.from(this).inflate(R.layout.ep2_my_layout, null);
        builder.setIcon(R.mipmap.ic_launcher).setView(inflate).create().show();
    }
}

第二题代码

package com.example.app2;

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

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "123321";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void click(View view) {
        switch (view.getId()) {
            case R.id.button_dialog:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setIcon(R.mipmap.ic_launcher).setTitle("标题").setMessage("消息。").create().show();
                break;
            case R.id.button_progress:
                final ProgressDialog pd = new ProgressDialog(this);
                pd.setMax(100);
                pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                final Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        pd.setProgress(pd.getProgress()+10);
                        if( pd.getProgress()==100){
                            timer.cancel();
                        }
                    }
                },0,500);
                pd.show();
                break;
            case R.id.button_my:
                AlertDialog.Builder bud = new AlertDialog.Builder(this);
                View inflate = LayoutInflater.from(this).inflate(R.layout.my_layout, null);
                bud.setIcon(R.mipmap.ic_launcher).setTitle("标题").setView(inflate).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
                    }
                }).create().show();
                break;
            case R.id.button_radio:
                final String[] strings = new String[]{ "111","222","333"};
                AlertDialog.Builder bud2 = new AlertDialog.Builder(this);
                bud2.setSingleChoiceItems(strings, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, strings[which], Toast.LENGTH_SHORT).show();
                    }
                }).setTitle("单选对黄框").setIcon(R.mipmap.ic_launcher).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i(TAG, "onClick: which"+which);
                    }
                }).create().show();
                break;
            case R.id.button_check:
                final String[] data={"大西几","小海疼","佩奇","乔治"};
                final boolean[] state={false,false,false,false};
                AlertDialog.Builder bud3 = new AlertDialog.Builder(this);
                bud3.setIcon(R.mipmap.ic_launcher).setTitle("标题").setMultiChoiceItems(data, state, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if( isChecked){
                            state[which]=true;
                        }
                    }
                }).setPositiveButton("提交", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        StringBuffer sb = new StringBuffer();
                        for (int i = 0; i < state.length; i++) {
                            if( state[i]){
                                sb.append(data[i]);
                            }
                        }
                        Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_SHORT).show();
                    }
                }).create().show();
                break;
            case R.id.button_mine:
                MyDialog md = new MyDialog(this);
                md.setMessage("消息啊啊啊").setTitle("标题啊啊啊").setYes(new MyDialog.YesOnClickListener() {
                    @Override
                    public void yesClick() {
                        Toast.makeText(MainActivity.this, "确定吗?", Toast.LENGTH_SHORT).show();
                    }
                });
                md.setNo(new MyDialog.NoOnClickListener() {
                    @Override
                    public void noClick() {
                        Toast.makeText(MainActivity.this, "确定小时吗?", Toast.LENGTH_SHORT).show();
                    }
                });
                md.create();
                md.show();
                break;

        }
    }
}
自定义对话框
package com.example.app2;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MyDialog extends Dialog {
    private TextView mineTextTitle;
    private TextView messages;
    private Button mineButtonSure;
    private Button mineButtonUnsure;

    private String title;
    private String message;
    public YesOnClickListener yes;
    public NoOnClickListener no;

    public interface YesOnClickListener{
        public void yesClick( );
    }

    public interface NoOnClickListener{
        public void noClick( );
    }

    public MyDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mine_layout);
        mineTextTitle = (TextView) findViewById(R.id.mine_text_title);
        messages = (TextView) findViewById(R.id.mine_text_message);
        mineButtonSure = (Button) findViewById(R.id.mine_button_sure);
        mineButtonUnsure = (Button) findViewById(R.id.mine_button_unsure);

        if( title!=null){
            mineTextTitle.setText(title);
        }
        if( message!=null){
            messages.setText(message);
        }
        if( yes!=null){
            mineButtonSure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    yes.yesClick();
                    dismiss();
                }
            });
        }
        if( no!=null){
            mineButtonUnsure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    no.noClick();
                    dismiss();
                }
            });
        }
    }

    public MyDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public MyDialog setMessage(String message) {
        this.message = message;
        return this;
    }

    public void setYes(YesOnClickListener yes) {
        this.yes = yes;
    }

    public void setNo(NoOnClickListener no) {
        this.no = no;
    }
}

第一题代码

package com.example.day2;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class Main2 extends AppCompatActivity {
    private Button but;
    private AlertDialog.Builder builder1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        but=findViewById(R.id.main2_button);
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Main2.this);
                builder.setIcon(R.mipmap.ic_launcher).setTitle("标题").setMessage("消息。")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(Main2.this, "点击了确定按钮", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(Main2.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create().show();
            }
        });
    }

    public void click(View view) {
        final String[] strs=new String[]{"00001","00002","00003","00004"};
        final boolean[] boo={false,false,false,false};
        final ProgressDialog pd;
        switch( view.getId()){
            case R.id.main2_button_radioButton:
                single();
                break;
            case R.id.main2_button_checkButton:

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setIcon(R.mipmap.ic_launcher).setTitle("表头")
                        .setMultiChoiceItems(strs, boo, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                if( isChecked){
                                    boo[which]=isChecked;
                                }
                            }
                        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        StringBuffer sb = new StringBuffer();
                        for (int i = 0; i < boo.length; i++) {
                            if( boo[i]){
                                sb.append(strs[i]+"\n");
                            }
                        }
                        Toast.makeText(Main2.this, sb.toString(), Toast.LENGTH_SHORT).show();
                    }
                }).create().show();
                break;
            case R.id.main2_button_alert:
                image();
                break;
            case R.id.main2_button_progress:
                progress();
                break;
            case R.id.main2_button_date:
                date_button();
                break;
            case R.id.main2_button_time:
                Calendar can = Calendar.getInstance();
                new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        Toast.makeText(Main2.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
                    }
                },can.get(Calendar.HOUR_OF_DAY),can.get(Calendar.MINUTE),false).show();
                break;
            case R.id.mine:
                MyDialog myDialog = new MyDialog(this);
                myDialog.setText("111111");
                myDialog.setText2("22222222");
                myDialog.setYesOnClickListener(new MyDialog.YesOnClickListener() {
                    @Override
                    public void yesOnClickListener() {
                        Toast.makeText(Main2.this, "确定。", Toast.LENGTH_SHORT).show();
                    }
                });
                myDialog.setNoOnClickListener(new MyDialog.NoOnClickListener() {
                    @Override
                    public void noOnClick() {
                        Toast.makeText(Main2.this, "取消啊啊啊", Toast.LENGTH_SHORT).show();
                    }
                });
                myDialog.show();
                break;
        }
    }

    private void date_button() {
        final Calendar can = Calendar.getInstance();
        DatePickerDialog dd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Toast.makeText(Main2.this, year+":"+month+":"+dayOfMonth, Toast.LENGTH_SHORT).show();
            }
        },can.get(Calendar.YEAR),can.get(Calendar.MONTH),can.get(Calendar.DAY_OF_MONTH));
        dd.show();
    }

    private void progress() {
        final ProgressDialog pd;
        pd = new ProgressDialog(this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setIcon(R.mipmap.ic_launcher);
        pd.setTitle("进度条");
        pd.setMax(100);
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if( pd.getProgress()==100){
                    timer.cancel();
                    Log.i("123321", "run: 完成了。");
                    pd.dismiss();
                }
                int num=pd.getProgress()+10;
                pd.setProgress(num);
            }
        },0,500);
        pd.show();
    }

    private void image() {
        View inflate = LayoutInflater.from(this).inflate(R.layout.my_layout, null);
        builder1 = new AlertDialog.Builder(this);
        builder1.setIcon(R.mipmap.ic_launcher).setTitle("标题").setView(inflate);
        final AlertDialog alertDialog = builder1.create();
        ImageView iv=inflate.findViewById(R.id.my_image);
        iv.setImageResource(R.mipmap.image);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        alertDialog.show();
    }

    private void single() {
        final String[] strings = {"11111","22222","33333"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher).setTitle("单选")
                .setSingleChoiceItems(strings, 1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(Main2.this, "选中了"+strings[which], Toast.LENGTH_SHORT).show();
                    }
                })
                .create().show();
    }
}
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:id="@+id/main2_button"
        android:text="普通对话框"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/main2_button"
        android:text="单选对话框"
        android:id="@+id/main2_button_radioButton"
        android:onClick="click"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/main2_button_radioButton"
        android:text="多选对话框"
        android:id="@+id/main2_button_checkButton"
        android:onClick="click"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/main2_button_checkButton"
        android:text="点击弹出自定义对话框"
        android:id="@+id/main2_button_alert"
        android:onClick="click"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="进度条"
        android:id="@+id/main2_button_progress"
        android:layout_above="@id/main2_button"
        android:onClick="click"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="日期选择对话框"
        android:layout_above="@id/main2_button_progress"
        android:id="@+id/main2_button_date"
        android:onClick="click"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="时间选择对话框"
        android:layout_above="@id/main2_button_date"
        android:id="@+id/main2_button_time"
        android:onClick="click"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义"
        android:layout_above="@id/main2_button_time"
        android:onClick="click"
        android:id="@+id/mine"
        />
</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值