程序员自我救赎

记录程序员救赎之道

一年多前就出现来这个技术,但是也没有怎么去了解和使用,一直以为EventBus和广播类似呢,后来才发现,EventBus更好用,类型更明确,发送的消息可以是任何自定义的对象,因此写个简单的使用,以免忘记

Github项目框架的地址:https://github.com/greenrobot/EventBus

使用步骤:

一、创建消息类(实体),用于存放消息的内容,例如:

1
2
3
4
5
6
7
8
9
10
class EventMessage{
String messge;
public void setMessge(String message){
this.message=message;
}
public String getMessage(){
return message;
}

}

二、注册消息:

EventBus.getDefault().register(this);

三、接受消息:

1
2
3
4
5
@Subscribe(threadMode = ThreadMode.MAIN)
public void showEventBuss(EventMessage event) {
String msg = "收到了消息:" + event.getMessage();
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}

其中方法名无所谓,内部运用了反射的机制,获取到方法名,进行的调用,ThreadMode有四种:
/**

  • Subscriber will be called in the same thread, which is posting the event. This is the default. Event delivery
  • implies the least overhead because it avoids thread switching completely. Thus this is the recommended mode for
  • simple tasks that are known to complete is a very short time without requiring the main thread. Event handlers
  • using this mode must return quickly to avoid blocking the posting thread, which may be the main thread.
    */
    POSTING,

/**

  • Subscriber will be called in Android’s main thread (sometimes referred to as UI thread). If the posting thread is
  • the main thread, event handler methods will be called directly. Event handlers using this mode must return
  • quickly to avoid blocking the main thread.
    */
    MAIN,在主线程中调用执行

/**

  • Subscriber will be called in a background thread. If posting thread is not the main thread, event handler methods
  • will be called directly in the posting thread. If the posting thread is the main thread, EventBus uses a single
  • background thread, that will deliver all its events sequentially. Event handlers using this mode should try to
  • return quickly to avoid blocking the background thread.
    */
    BACKGROUND,在子线程中执行

/**

  • Event handler methods are called in a separate thread. This is always independent from the posting thread and the
  • main thread. Posting events never wait for event handler methods using this mode. Event handler methods should
  • use this mode if their execution might take some time, e.g. for network access. Avoid triggering a large number
  • of long running asynchronous handler methods at the same time to limit the number of concurrent threads. EventBus
  • uses a thread pool to efficiently reuse threads from completed asynchronous event handler notifications.
    */
    ASYNC

四、注销消息:

EventBus.getDefalt().unregister(this);

Android Fragment 的认知学习

由于工作中很少用到fragment 导致fragment and fragmentActivity and FragmLayout 的混淆,特此学习笔记一下。

Fragment 与FragmentActivity

Fragment是一种具有生命周期的View,能够使用Fragmentmanager进行操作
FragmentActivity是继承自Activity的子类,为兼容4.0以下版本使用的。4.0以上版本还是使用Activity的其他类型进行管理Fragment。

生命周期:

new Fragement():

onAttach -> onCreate -> onCreatView -> onActivityCreated -> onStart -> onResume

manager.remove(fragment):

onPause -> onStop -> onDestory -> onDetach

manager.add(fragment):

manager.replace:
onPause -> onStop -> onDestoryView -> onDestory -> onDetach -> (新的Fragment )onAttach -> onCreate -> onCreatView -> onActivityCreated -> onStart -> onResume

FrameLayout

是一种继承自ViewGroup的控件,能够放置很多的子控件,并且叠加在一起
可以作为容器被activity进行操作放置Fragment对象。

最近工作中用到了动态设置EditText控件的内容:

动态设置EditText的输入格式需要在最后设置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
editText = new EditText(context);
editText.setBackground(null);
editText.addTextChangedListener(this);
editText.setHint(mHint);
editText.setHintTextColor(getResources().getColor(R.color.cellview_edite_texthintcolor));
editText.setTextColor(getResources().getColor(R.color.cellview_edite_textcolor));
editText.setTextSize(DisplayUtil.dip2px(context, DEFALT_TEXT_SIZE));
editText.setGravity(Gravity.RIGHT);

if (length > 0) {
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)});
}
//

editText.setSingleLine();
editText.measure(0, 0);
w = DisplayUtil.getScreenWith(context) - w - 2 * padLeft;
h = editText.getMeasuredHeight();
MarginLayoutParams editTextmp = new MarginLayoutParams(w
, h); //item的宽高
editTextmp.setMargins(0, mt, padLeft, 0);//分别是margin_top那四个属性
RelativeLayout.LayoutParams editeparams = new RelativeLayout.LayoutParams(editTextmp);
editeparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
// editeparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
editText.setLayoutParams(editeparams);
if (isPassword) {
// editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
addView(editText);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void main(String[] args) {
String s1 = "善南街道善国苑善国苑24号楼12单元14层1402";
String s2 = "枣庄滕州市龙泉街道苹果花园苹果花园9号楼3单元18层144023";
String s3 = "枣庄滕州市张汪镇邱仓/闫道沟邱仓/闫道沟";
String s = "枣庄滕州市西岗镇西岗镇沿街市场西岗镇沿街市场423号楼34单元12342号箱10235";
int l = 2;
String congfu="";
for (int i = 0; i < s.length() - l; i++) {
String ss = s.substring(i, i + l + 1);
String sss = s.replaceFirst(ss, "");
if (sss.contains(ss)) {
congfu=ss;
l++;
i--;
}
}
System.out.println(congfu);
s=s.replaceFirst(congfu, "");
System.out.println(s);
}

注意:字符串中含有“\”时

1
2
string.contains("\\");
string.replace("\\\\","");

JAVA操作excel表格:
表格内容的复制(从一个文件复制某表格到另一个文件中):

步骤一:获得两个文件对象,其中book2是打开book1的缓冲

1
2
3
4
book = Workbook.getWorkbook(file);
book1 = Workbook.getWorkbook(new File(FileUtils.getExternalStoragePath(), name + "_changed.xls"));
book2 = Workbook.createWorkbook(new File(FileUtils.getExternalStoragePath(), name + "_changed.xls"), book1);

步骤二:获取表格对象

1
2
3
4
//获取原文件中的某表格对象
Sheet sheet = book.getSheet(0);
//获取写入文件中的某表格对象
WritableSheet sheet2 = book2.getSheet(sheetLocation);

步骤三:获取表格中的内容,并把内容写进表2中(icolumn是某列,row是某行)

1
2
3
4
5
6
7
8
9
10
Cell cell = sheet.getCell(column, row);
//1、把字符串写入表中
String result = cell.getContents();
sheet2.addCell(new Label(column, row, result));
//2、把Number写入表中:
String result = cell.getContents();
sheet2.addCell(new Number(column, row, Double.parseDouble(result)));
//2、把日期格式的内容写入表中:
DateCell dc = (DateCell) cell;
sheet2.addCell(new DateTime(dc).copyTo(column,row));

步骤四:最后关闭表格

1
2
3
4
5
6
7
8
9
10
11
if (book2 != null) {
book2.write();
book2.close();
}
if (book1 != null) {
book1.close();
}

if (book != null) {
book.close();
}

附:给表格加边框

1
2
3
4
5
6
7
8
//给表格加边框的format
Label label = new Label(0, 0, "");
CellFormat s = label.getCellFormat();
wcf = new WritableCellFormat(s);
wcf.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);

//使用时:
sheet2.addCell(new Label(column, row, result,wcf));

Mac终端的命令

lilifengdeMacBook-Pro:Library limxing$ cd Android //进入某个文件夹

lilifengdeMacBook-Pro:tools limxing$ sudo -s //获取权限

bash-3.2# ls //获取这个目录下的所有文件

bash-3.2# chmod +x * //获取这个目录的权限解决Permission denied的问题
bash-3.2# ./android sdk

2、终端的使用环境配置
echo $PATH 查看PATH
touch .bash_profile
open .bash_profile
export PATH=”$HOME/.rbenv/bin:$PATH” 添加修改配置,完成后保存
source .bash_profile 生效

结论:手机会先找自己对应的layout-dpi文件,如果没有就会先向高精度的文件夹下查找,高的都没有就会想精度低的查找。

drawable-ldip
drawable-mdip
drawable-hdip
drawable-xhdip
drawable-xxhdip
drawable-xxxhdip
手机对应hdip,如果没有相应的文件:顺序是xh–xxh–xxxh–m-l;同样的values同理。

FragmentActivity与Activity

FragmentActivity是为兼容4.0一下系统而需要的Activity类

Fragment类的生命周期

new:onAttach()–>onCreat()–>onCreatView()–>onStart()–>onresume
remove:onPause()–>onStop()–>onDestoryView()–>onDestory()–>onDetach()

使用:再Activity或FragmentActivity中获取FragmentManager

0%