Android自定义Dialog,Toast,Notification和PopupWindow

本文介绍了在Android应用中使用Toast、Notification以及自定义PopupWindow的功能,并探讨了如何通过合理的学习路径提升技术水平,达到高薪职位。
摘要由CSDN通过智能技术生成

TextView toast_tv = (TextView) myView

.findViewById(R.id.toast_tv);

TextView toast_time = (TextView) myView

.findViewById(R.id.toast_time);

SpannableString spannableString = new SpannableString(

“Hello,Android! -zhoumushui”);

spannableString.setSpan(new ForegroundColorSpan(Color.YELLOW),

0, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

spannableString.setSpan(new ForegroundColorSpan(Color.CYAN),

14, spannableString.length(),

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

toast_tv.setText(spannableString);

// SimpleDateFormat sdf = new

// SimpleDateFormat(“yyyy-MM-dd HH:mm:ss:SSS”);

SimpleDateFormat sdf = new SimpleDateFormat(

“yyyy年MM月dd日 HH:mm:ss:SSS”);

toast_time.setText(sdf.format(new Date()));

Toast myToast = new Toast(Main.this);

myToast.setDuration(Toast.LENGTH_SHORT);

myToast.setView(myView);

myToast.setGravity(Gravity.CENTER, 0, 0);

myToast.show();

} else if (v == btn_notify) {

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification = new Notification(

R.drawable.ic_launcher, “Hello,Android!–zhoumushui”,

System.currentTimeMillis());

notification.flags = Notification.FLAG_AUTO_CANCEL;

Intent intent = new Intent(Main.this, AfterClickNotify.class);

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP

| Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent contentIntent = PendingIntent

.getActivity(Main.this, 0, intent,

PendingIntent.FLAG_UPDATE_CURRENT);

notification.setLatestEventInfo(Main.this, “Hello,Android!”,

“zhoumushui.”, contentIntent);

notificationManager.notify(R.string.app_name, notification);

}

}

}

有于只是演示,点击Notification后跳转到的Activity只是定义了布局,没做复杂处理:

public class AfterClickNotify extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.afterclicknotify);

}

}

布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical” >

<TextView

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:background=“#300000ff”

android:text=“Hello Android\nZhou Mu Shui\nblog.csdn.net/zhoumushui”

android:textSize=“25dp” />

好了,今天的内容比较基础,就先这样了,改日再写数据存储的内容。

代码工程下载

自定义PopupWindow:

效果图如下,类似iOS的ActionSheet:

核心代码:

package com.zms.actionsheet;

import android.app.Dialog;

import android.content.Context;

import android.content.DialogInterface.OnCancelListener;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.Window;

import android.view.WindowManager;

import android.widget.LinearLayout;

import android.widget.TextView;

public class ActionSheet {

public interface OnActionSheetSelected {

void onClick(int whichButton);

}

private ActionSheet() {

}

public static Dialog showSheet(Context context, final OnActionSheetSelected actionSheetSelected,

OnCancelListener cancelListener) {

final Dialog dlg = new Dialog(context, R.style.ActionSheet);

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.actionsheet, null);

final int cFullFillWidth = 10000;

layout.setMinimumWidth(cFullFillWidth);

TextView mContent = (TextView) layout.findViewById(R.id.content);

TextView mCancel = (TextView) layout.findViewById(R.id.cancel);

mContent.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

actionSheetSelected.onClick(0);

dlg.dismiss();

}

});

mCancel.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

actionSheetSelected.onClick(1);

dlg.dismiss();

}

});

Window w = dlg.getWindow();

WindowManager.LayoutParams lp = w.getAttributes();

lp.x = 0;

final int cMakeBottom = -1000;

lp.y = cMakeBottom;

lp.gravity = Gravity.BOTTOM;

dlg.onWindowAttributesChanged(lp);

dlg.setCanceledOnTouchOutside(false);

if (cancelListener != null)

dlg.setOnCancelListener(cancelListener);

dlg.setContentView(layout);

dlg.show();

return dlg;

}

}

仿微信电话本短信标题的PopupWindow

刚刚看了部电影《Rush》,作为F1迷很是喜欢这种feel,让我想起了Ayrton Senna的那个年代,遗憾的是一味追求速度,难免会有悲剧伴随。

好了,步入正题。

在项目中我不是用PopupWindwow实现的,当时我的做法是利用布局的可见性,结合代码设置,虽然实现了同样效果,但是比较麻烦,并且不便于后期维护。

最近写了个小Demo,发现用PopupWindow实现方便很多,今天总结一下:

布局文件有两个:主文件布局main.xml和对应的popwindow.xml,布局都比较简单,也没有美化。主要逻辑都在代码里。

main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:background=“#ffffff”

android:orientation=“vertical” >

<LinearLayout

android:layout_width=“fill_parent”

android:layout_height=“50dp”

android:background=“#454545”

android:orientation=“horizontal”

android:weightSum=“5” >

<ImageButton

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:background=“@null”

android:src=“@drawable/icon” />

<LinearLayout

android:layout_width=“wrap_content”

android:layout_height=“fill_parent”

android:layout_weight=“3”

android:onClick=“ShowOrHidePopup”

android:weightSum=“3” >

<TextView

android:id=“@+id/tv_topbar”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:layout_weight=“1”

android:gravity=“center”

android:text=“短信”

android:textSize=“20dp” />

<ImageButton

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:background=“@null”

android:src=“@drawable/icon” />

popwindow.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:background=“#454545”

android:orientation=“vertical” >

<View

android:layout_width=“fill_parent”

android:layout_height=“1px”

android:background=“#000000” />

<LinearLayout

android:layout_width=“fill_parent”

android:layout_height=“90dp”

android:orientation=“horizontal” >

<TextView

android:layout_width=“wrap_content”

android:layout_height=“90dp”

android:drawableTop=“@drawable/icon”

android:layout_weight=“1”

android:gravity=“center”

android:text=“收藏短信”

/>

<View

android:layout_width=“1px”

android:layout_height=“95dp”

android:layout_gravity=“center_vertical”

android:background=“#000000”

/>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“90dp”

android:drawableTop=“@drawable/icon”

android:layout_weight=“1”

android:gravity=“center”

android:text=“广告垃圾”

/>

代码也不长,很方便使用的一个控件。主要就是用一个LayoutInflater把popwindow.xml加载到一个View中,然后用PopWindow的对象加载这个View,并且设置layout样式,剩下的没什么区别了。

package com.example.popupwindow;

import android.app.Activity;

import android.os.Bundle;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.Window;

import android.view.WindowManager.LayoutParams;

import android.widget.Button;

import android.widget.PopupWindow;

import android.widget.RadioGroup;

import android.widget.TextView;

public class MyPopupWindowDemo extends Activity {

private Button popbut = null; // 呼出状态Button

private RadioGroup changestatus = null; //

private TextView statusinfo = null;

private Button cancel = null;

private PopupWindow popWin = null;

private View popView = null;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

super.setContentView(R.layout.main);

LayoutInflater inflater = LayoutInflater.from(this);

popView = inflater.inflate(R.layout.popwindow, null);

popWin = new PopupWindow(popView, LayoutParams.FILL_PARENT,

LayoutParams.WRAP_CONTENT);

}

public void ShowOrHidePopup(View v) {

if (popWin.isShowing()) {

popWin.dismiss();

} else {

popWin.showAtLocation(v, Gravity.NO_GRAVITY, 0, 100);

}

}

}

ShowOrHide函数里做了个判断,isShowing是该PopWindow对象当前是否显示,dismiss()函数就是把PopWindow给隐藏,showAtLocation则是在指定位置以指定Gravity显示该PopWindow。这就是Android的便捷之处,什么接口都是现成的,直接调用就行。当然,这只是实现最基本的功能,想要更好的效果自然也得费点心思。

最后

那我们该怎么做才能做到年薪60万+呢,对于程序员来说,只有不断学习,不断提升自己的实力。我之前有篇文章提到过,感兴趣的可以看看,到底要学习哪些知识才能达到年薪60万+。

通过职友集数据可以查看,以北京 Android 相关岗位为例,其中 【20k-30k】 薪酬的 Android 工程师,占到了整体从业者的 30.8%!

北京 Android 工程师「工资收入水平 」

今天重点内容是怎么去学,怎么提高自己的技术。

1.合理安排时间

2.找对好的系统的学习资料

3.有老师带,可以随时解决问题

4.有明确的学习路线

当然图中有什么需要补充的或者是需要改善的,可以在评论区写下来,一起交流学习。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

果自然也得费点心思。

最后

那我们该怎么做才能做到年薪60万+呢,对于程序员来说,只有不断学习,不断提升自己的实力。我之前有篇文章提到过,感兴趣的可以看看,到底要学习哪些知识才能达到年薪60万+。

通过职友集数据可以查看,以北京 Android 相关岗位为例,其中 【20k-30k】 薪酬的 Android 工程师,占到了整体从业者的 30.8%!

北京 Android 工程师「工资收入水平 」

[外链图片转存中…(img-XYj0RrCX-1714451816335)]

今天重点内容是怎么去学,怎么提高自己的技术。

1.合理安排时间

2.找对好的系统的学习资料

3.有老师带,可以随时解决问题

4.有明确的学习路线

当然图中有什么需要补充的或者是需要改善的,可以在评论区写下来,一起交流学习。

[外链图片转存中…(img-jkXRVHJs-1714451816335)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 8
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值