自定义Dialog DatePicker,TimerPicker PopupWindow Notification

自定义Dialog

书接昨天的dialog,在此只写一个方法

private void costomDialog() {
        dialog = new Dialog(MainActivity.this, R.style.NoDialogTitle);
        LayoutInflater inflater = getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.my_dialog, null);
        TextView textview_title = (TextView) dialogView.findViewById(R.id.dialog_title);
        TextView textview_message = (TextView) dialogView.findViewById(R.id.dialog_message);
        Button button_cancel = (Button) dialogView.findViewById(R.id.button_cancel);
        Button button_ok = (Button) dialogView.findViewById(R.id.button_ok);
        textview_title.setText("潘家园");
        textview_message.setText("潘家园旧货市场位于北京东三环南路潘家园桥西南,大型古玩艺术品市场。");
        button_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        button_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "点击的是确定", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(dialogView);
        dialog.show();
    }

布局文件

<?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"
    android:background="@drawable/dialog_background">
    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="这是标题"
        style="@style/textview"/>
    <TextView
        android:id="@+id/dialog_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这是内容"
        android:padding="10dp"
        android:gravity="center"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/button_cancel"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="取消"
            android:background="@drawable/btn_background"/>
        <Button
            android:id="@+id/button_ok"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="确定"
            android:background="@drawable/btn_right_background"/>
    </LinearLayout>

</LinearLayout>

drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="15dp"/>
    <solid android:color="#ffffff"/>
    <stroke android:color="#ffffff" android:width="2dp"/>

</shape>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_right_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_right_normal"/>
</selector>

style

<style name="textview">
        <item name="android:textColor">#32cd32</item>
        <item name="android:padding">15dp</item>
        <item name="android:background">@drawable/title_background</item>
    </style>

DataPicker,TimerPicker

也是接昨天的程序,这里只写两个方法

private void dataPicker() {
        mCalendar = Calendar.getInstance();
        DatePickerDialog dialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
                mCalendar.set(year,monthOfYear,dayOfMonth);
                SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
                Toast.makeText(getApplicationContext(), "此处是造型的" + format.format(mCalendar.getTime()), Toast.LENGTH_SHORT).show();
            }
        },mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH),mCalendar.get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }
private void timerPicker() {
        mCalendar = Calendar.getInstance();
        TimePickerDialog dialog1 = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
                mCalendar.set(Calendar.HOUR,hourOfDay);
                mCalendar.set(Calendar.MINUTE,minute);
                SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日HH:mm");
                Toast.makeText(getApplicationContext(), "此处是造型的" + format.format(mCalendar.getTime()), Toast.LENGTH_SHORT).show();
            }
        },mCalendar.get(Calendar.HOUR),mCalendar.get(Calendar.MINUTE),true);
        dialog1.show();
    }

点击某处时,会有一个下拉的view,里面是详细信息
接昨天的程序,在button2下写的。
去掉边框的设置:
mPopupWindow.setBackgroundDrawable(null);

public boolean onKeyDown(int keyCode,KeyEvent event){
        if(keyCode==KeyEvent.KEYCODE_BACK){
            if(mPopupWindow!=null&&mPopupWindow.isShowing()){
                mPopupWindow.dismiss();
                return true;
            }
        }
        return  super.onKeyDown(keyCode,event);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1:
                buttonOne();
                break;
            case R.id.button2:
//                buttonTwo();
                mPopupWindow = new PopupWindow(MainActivity.this);
                View popView = getLayoutInflater().inflate(R.layout.my_pop,null);
                mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
                mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                mPopupWindow.setContentView(popView);
                mPopupWindow.setFocusable(false);
//                mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.ic_launcher));
                mPopupWindow.setOutsideTouchable(true);
                mPopupWindow.showAsDropDown(mBtn2);
                break;

Notification

消息提醒框,

public class MainActivity extends Activity implements View.OnClickListener{
    private Button mBtn1;
    private Button mBtn2;
    private NotificationManager mNotificationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//初始化NotificationManager
        mBtn1 = (Button) findViewById(R.id.button1);
        mBtn2 = (Button) findViewById(R.id.button2);
        mBtn1.setOnClickListener(this);
        mBtn2.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1:
                Notification notification = new Notification();//初始化notification
                notification.icon = R.mipmap.ic_launcher;//设置通知的图片
                notification.tickerText = "您有新短消息";//设置状态栏文本
                notification.flags = Notification.FLAG_AUTO_CANCEL;//设置为可以取消
                Intent intent = new Intent(getApplicationContext(),MainActivity.class);//设置pendingintent事件
                PendingIntent pend = PendingIntent.getActivity(getApplicationContext(),1,intent,PendingIntent.FLAG_ONE_SHOT);//设置pendingintent的使用方式
                notification.setLatestEventInfo(getApplicationContext(), "这是标题", "这是内容", pend);//设置pendingintent显示的内容
                notification.when = System.currentTimeMillis();//Calendar.getInstance().getTimeInMillis();设置出发时间
                mNotificationManager.notify(2,notification);//通知管理器将通知添加
                break;
            case R.id.button2:
//                mNotificationManager.cancel(1);//取消id为1的通知
                Intent intent1 = new Intent(getApplicationContext(),MainActivity.class);
                PendingIntent pend1 = PendingIntent.getActivity(getApplicationContext(),1,intent1,PendingIntent.FLAG_ONE_SHOT);//设置pendingintent的使
                Notification notification1 = new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher)
                        .setTicker("新短消息").setContentTitle("标题").setContentText("文本").setContentInfo("内容").setContentIntent(pend1)
                        .setAutoCancel(true).setWhen(System.currentTimeMillis()).build();
                mNotificationManager.notify(2,notification1);
                break;
            default:
                break;
        }
    }

自定义dialog布局设置

drawable下建一个xml文件,设置shape,其中三个属性corners,solid,stroke分别设置角、颜色、边框

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:bottomLeftRadius="15dp"/>
    <solid android:color="#79dff6"/>
    <stroke android:color="#ffffff" android:width="2dp"/>
</shape>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值