1、获取系统当前时间(以年月日为例):yyyy-MM-dd对应格式:2017-01-01,根据需求自己改变格式
public String getDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new java.util.Date());
}
——————————————————————————————————————————————————
设置自动弹出软键盘:
et_new_value.setFocusable(true);
et_new_value.setFocusableInTouchMode(true);
et_new_value.requestFocus();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) et_new_value.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
}
}, 200);
——————————————————————————————————————————————————
1、设置EdiText的光标的颜色以及大小:
添加属性:
android:textCursorDrawable="@drawable/color_cursor"
自定义一个光标color_cursor:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:width="1.5dp" />
<solid android:color="#7B3A94" />
</shape>
————————————————————————————————————————————————————————————
2、让EditText默认事先失去焦点,不让光标闪动:
找到EditText的父控件设置:
android:focusable="true"
android:focusableInTouchMode="true"
———————————————————————————————————————————————————————————
3、ListView设置空View:
if(addressAdapter==null||addressAdapter.isEmpty()){
View empityView = View.inflate(this,R.layout.empty,null);
((ViewGroup) delivery_address_listView.getParent()).addView(empityView, delivery_address_listView.getLayoutParams());
delivery_address_listView.setEmptyView(empityView);
}
—————————————————————————————————————————————————————————
4、强制所有页面竖屏:
可以在你的基类里添加:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
——————————————————————————————————————————————————————————
5、TextView设置下划线:
textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG );
textView.getPaint().setAntiAlias(true);
——————————————————————————————————————————————————————————
6.日历控件CalendarView使用:
1、获取系统当前时间并设置成yyyy-M-d格式(比如2016-1-1)
depart_time.setText(new SimpleDateFormat("yyyy-M-d").format(new Date()));
2、设置允许起始日期为当前日期
calendarView.setMinDate(Calendar.getInstance().getTimeInMillis());
3、month月份注意+1
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
depart_time.setText(year+"-"+(month+1)+"-"+dayOfMonth);
}
});
——————————————————————————————————————————————————————————
7.系统AlertDialog设置按钮字体颜色大小(非自定义Dialog):
private AlertDialog dialog;
public void showDialog(View view){
dialog = new AlertDialog.Builder(this).create();
dialog.setMessage("确定要删除?");
dialog.setButton(AlertDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"删除",Toast.LENGTH_SHORT).show();
}
});
dialog.setButton(AlertDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
//注意:设置的这些属性(字体颜色、大小)都要在show之后,不然报空指针
Button btnPositive = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
Button btnNegative = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
btnPositive.setTextColor(Color.parseColor("#7B3A94"));
btnNegative.setTextColor(Color.parseColor("#7B3A94"));
}
——————————————————————————————————————————————————————————
8.SpannableString设置String给TextView分段显示不同的颜色:
String price = "售价: ¥585";
比如我想让前面的“售价:”显示的是黑色,后面的价格“¥585”显示的是红色
就可以在item中设置TextView的时候字体颜色先全部设置成红色
然后在adapter中绑定数据的时候设置
SpannableString ss_price= new SpannableString(price);
ss_price.setSpan(new ForegroundColorSpan(Color.parseColor("#131313")),0,3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//设置文本的前景色
item.wine_selling_price.setText(ss_price);
————————————————————————————————————————————————————————————
9、Android系统为了跟IOS不一样,当界面OverScroll的时候会显示一个阴影。为了达到更好的显示效果,最好禁用系统的overScrollMode,如根布局添加android:overScrollMode=”never”
————————————————————————————————————————————————————————————
10、关于字节byte、进制以及IP的转换问题
/** * byte字节数组转换成16进制 */
public String toHexString(byte[] bytes) {
BigInteger bigInteger = new BigInteger(1, bytes);
return bigInteger.toString(16);
}
/** * 16进制转IP */
public String HEXToIp(String hex){
//要先将其转换成10进制long整型,再转换成ip;
return longToIP(Long.parseLong(hex,16));
}
/** * 10进制long类型整数,转换为 IP */
public String longToIP(long longIp) {
StringBuffer sb = new StringBuffer("");
// 直接右移24位
sb.append(String.valueOf((longIp >>> 24)));
sb.append(".");
// 将高8位置0,然后右移16位
sb.append(String.valueOf((longIp & 0x00FFFFFF) >>> 16));
sb.append(".");
// 将高16位置0,然后右移8位
sb.append(String.valueOf((longIp & 0x0000FFFF) >>> 8));
sb.append(".");
// 将高24位置0
sb.append(String.valueOf((longIp & 0x000000FF)));
return sb.toString();
}
/** * Ip 转换为 long类型 10 进制整数 */
public Long ipToLong(String ipString) {
Long[] ip = new Long[4];
int pos1= ipString.indexOf(".");
int pos2= ipString.indexOf(".",pos1+1);
int pos3= ipString.indexOf(".",pos2+1);
ip[0] = Long.parseLong(ipString.substring(0 , pos1));
ip[1] = Long.parseLong(ipString.substring(pos1+1 , pos2));
ip[2] = Long.parseLong(ipString.substring(pos2+1 , pos3));
ip[3] = Long.parseLong(ipString.substring(pos3+1));
return (ip[0]<<24)+(ip[1]<<16)+(ip[2]<<8)+ip[3];
}
/** * 将16进制Id转换成10进制Id */
private String SixTeenToTen(String each_collecter) {
String eachId = each_collecter.substring(0, 8);
String a = eachId.substring(0, 2);
String b = eachId.substring(2, 4);
String c = eachId.substring(4, 6);
String d = eachId.substring(6, 8);
return String.valueOf(Integer.parseInt(a, 16) + Integer.parseInt(b, 16) * 256 +
Integer.parseInt(c, 16) * 256 * 2 + Integer.parseInt(d, 16) * 256 * 3);
}
————————————————————————————————————————————————————————————
11、子线程中不能直接弹吐司:可以这样弹
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
Toast.makeText(MainActivity.this,"Hello",Toast.LENGTH_SHORT).show();
Looper.loop();
}
}).start();
———————————————————————————————————————————————————————————
12、ListView定位问题:
当打开列表的其中一项,然后返回时 需要定位到之前滚动到的位置
private int index;
private int top;
//在列表点击事件setOnItemClickListener里记录滚动到的位置
index = listView.getFirstVisiblePosition();
View v = listView.getChildAt(0);
top = (v == null) ? 0 : v.getTop();
//当返回时,再恢复到原来的位置
listView.setSelectionFromTop(index, top);