程序员自我救赎

记录程序员救赎之道

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

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

/**
* Created by limxing on 16/1/7.
*/
public class LoadView extends ImageView {
private float degrees = 0f;
private Matrix max;
private int width;
private int height;
private Bitmap bitmap;

public LoadView(Context context) {
super(context);
init();
}

public LoadView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public LoadView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}

Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
degrees += 30f;
max.setRotate(degrees, width, height);
setImageMatrix(max);
if(degrees==360){
degrees=0;
}
}
};

private void init() {
setScaleType(ScaleType.MATRIX);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.loading);
setImageBitmap(bitmap);
max = new Matrix();

width = bitmap.getWidth() / 2;
height = bitmap.getHeight() / 2;
Timer time=new Timer();
time.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0);
}
},0,80);
}
}

0%