关于教务系统项目所遇到的一些知识点总结
##关于详细的爬取学校系统的过程我将再写一篇博客如下:
(暂无)
1.Activity跳转Activity
方法一
Intent intent = new Intent(当前Activity.this, 目标Activity.class);
putExtra("标志",数据);//当需要在跳转时传递数据用这个语句
startActivity(intent);
2.Activity跳转Fragment
####方法一
Intent intent = new Intent(Activity.this ,Fragment所在Activity.class);
putExtra("标志",int); // 用来标记要跳转的是哪个fragment
startActivity(intent);
//----------------------------------------------------------------------------
//然后在跳转的Activity里这样做:
Intent intent = getIntent();
intent.getIntExtra("标志");
//然后切换
方法二
Intent intent = new Intent(Activity.this ,Fragment所在Activity.class);
startActivity(intent);
//直接在Fragment所在Activity里处理切换fragment的逻辑
//当然这个方法的前提是你已经知道要切换的Fragment是谁了
3.Fragment跳转Activity
Intent intent = new Intent(getActivity(), 目标Activity.class);
startActivity(intent);
4.java代码动态生成View/ViewGroup并添加
LinearLayout
//动态生成lly这个LinearLayout这个ViewGroup
LinearLayout lly = new LinearLayout(getContext()); // 获取上下文context
lly.setOrientation(LinearLayout.VERTICAL); //方向竖直
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT ,firstHeight); //设置宽和高
// 可以通过这个addRule添加规则:第一个参数为方位,第二个参数为相对于哪个view的id
params.addRule(RelativeLayout.RIGHT_OF ,left_top_Text.getId()); //设置位置为在left_top_Text这个TextView的右边
lly.setLayoutParams(params);
父容器.addView(lly); //必须有这一步,否则没有效果,因为没有添加进去
ScrollView
注意:ScrollView只能有一个子view(当然你可以把你想要写的多个view用Layout包起来)
ScrollView scrollView = new ScrollView(getContext());
scrollView.setId(R.id.scrollView);
LayoutParams rlp_sv = new LayoutParams(LayoutParams.MATCH_PARENT , LayoutParams.WRAP_CONTENT);
//如果你需要设置它相对于某个控件的位置就可以使用下面这条语句和上面Layout的用法一样
rlp_sv.addRule(RelativeLayout.BELOW ,left_top_Text.getId());
scrollView.setLayoutParams();
父容器.addView(lly); //必须有这一步,否则没有效果,因为没有添加进去
FramLayout
FraeLayout frameLayout = new FrameLayout(getContext());
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT );
frameLayout.setLayoutParams(layoutParams);
父容器.addView(lly); //必须有这一步,否则没有效果,因为没有添加进去
TextView
TextView tv = new TextView(getContext());
tv.setHeight(height);
tv.setWidth(width);
tv.setTextColor(getResources().getColor(R.color.text1_color)); //这里引用了一个values中colors.xml里的自定义颜色
tv.setTextSize(14);
tv1.setGravity(CENTER_HORIZONTAL); //在LinearLayout的VERTICAL条件下
tv.setText("内容") ;
父容器.addView(lly); //必须有这一步,否则没有效果,因为没有添加进去
ProgressDialog
这里直接给出了ProgressDialog的封装好的开启和关闭的方法!
//开启
private void showPressDialog(){
if(progressDialog == null){
progressDialog = new ProgressDialog(this);
progressDialog .setMessage("正在加载...");
progressDialog.setCanceledOnTouchOutside(false);
}
progressDialog.show();
}
//关闭
private void closeProgressDialog(){
if(progressDialog != null){
progressDialog.dismiss();
}
}
4.Fragment切换Fragment
//v4
getSupportFragmentManager().beginTransaction;
.replace(R.id.frame_content, new CenterFragment());
.commit();
5.指定x,y坐标生成view
LayoutParams params = new LayoutParams(x,y);//x,y分别为距离左边屏幕和上边屏幕的距离
控件对象.setLayoutParams(params);
6.自定义背景Drawable
这里是我用到的一些语法
<?xml version="1.0" encoding="utf-8">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="0dp"> // 一个item相当于一个独立的drawable
<shape android:shape="rectangle"> //shape表示形状,rectange为矩形
<solid android:color = "#344AF7"/> //solid表示纯色填充,通过android:color设置颜色
<corners android:topRightRadius ="10dp" //为右上角设定角度(圆角)
android:topLeftRadius ="10dp"/> //为左上角设定角度(圆角)
</shape>
</item>
<item android:bottom = "0.5dp" //与上边的距离
android:right="0.5dp" //与右边的距离
android:left="0.5dp" > //与左边的距离
<shape android:shape="rectangle">
<corners android:topRightRadius ="10dp"
android:topLeftRadius ="10dp"/>
<solid android:color ="#cbeff6"/>
</shape>
</item>
</layer-list>
7.viewPager+tablayout
这个之前博客有这里给链接:
viewPager+tabLayout的代码
viewPager+tabLauyout的讲解
8.Okhttp的使用
导包:implementation ‘com.squareup.okhttp3:okhttp:3.4.1’
模式:builder.build() 建造者模式
封装的一个工具类HttpUtil如下:
> public class HttpUtil {
public static void sendOkHttpRequest(Request request,okhttp3.Callback callback ){
OkHttpClient client = new OkHttpClient();
client.connectTimeoutMillis();
client.newCall(request).enqueue(callback);
}
}
然后举登陆为例子:
>//post请求 request = 表单+头
>这个是body表单
>FormBodgy formBody = new FormBody.Builder( )
.add("__VIEWSTATE", "dDwxNTMxMDk5Mzc0Ozs+lYSKnsl/mKGQ7CKkWFJpv0btUa8=" )
.add("txtUserName", xh)
.add("Textbox1", mm)
.add("TextBox2", mm)
.add("txtSecretCode", checkCode)
.add("RadioButtonList1","%D1%A7%C9%FA" )
.add("Button1", "")
.add("lbLanguage","")
.add("hidPdrs", "")
.add("hidsc", "")
.build();
//这个是头head
Request.Builder builder = new Request.Builder();
final Request request = builder
.url(StringPool.ORIGIN)
//.url(StringPool.URL_LOGIN )
.addHeader("Cookie", StringPool.COOKIE)
.addHeader("Origin",StringPool.ORIGIN)
.addHeader("Host",StringPool.HOST)
.addHeader("Referer",StringPool.URL_LOGIN)
.post(formBody)
.build();
//带着head和body网络请求
HttpUtil.sendOkHttpRequest(request, new Callback( ) {
@Override
public void onFailure(Call call, IOException e) {
closeProgressDialog();
Toast.makeText(context ,"登录失败!",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.e("msg","登陆成功!");
//String urlString = response.request().toString();// 登陆网址
String urlContent = response.body().string(); // 登陆内容
....这里进行解析跳转页面等等的逻辑
Toast.makeText(context,"登陆成功!",Toast.LENGTH_SHORT ).show();
}
});
9.Jsoup解析使用
导包:implementation ‘org.jsoup:jsoup:1.10.3’
在网络请求成功的逻辑里面写或者使用handler在handler的重写方法里面写:
>Document doc= Jsoup.parse(response.body().string());
Elements elements = doc.select("table")//select标签过滤
.attr("class" , "datalist")//attr获取属性
.select("tr")
.attr("class","alt");
for(int i = 5 ; i < elements.size() -1; i++){
String text = elements.get(i).text();
String[] data = text.split(" ");
}
更详细的使用在官方文档:jsoup开发指南
10.Handler的使用
这里使用的都是主线程的handler。
import android.os.Handler;
为什么要使用handler?
解决异步,比如网络请求完使用handler来更新UI
消息通知,比如解析完数据使用handler来更新UI内容或者做一些你想要做的操作。
使用原则:重写public void handleMessage(Message msg)方法。
有2种方式(举例网络请求):
1)直接在请求成功后:
> Message msg = Message.obtain();
msg.what = 2;//这个是标志
msg.obj = balbala //这里放你要传递的信息
handler.sendMessage(msg);
在同一个java里的定义一个全局的Handler
>@SuppressLint("HandlerLeak")
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 2) {
//这里进行更新ui操作
scoreAdapter.notifyDataSetChanged( );//这个是刷新adapter
}
}
};
2)创建自己的类
>public class MyHandler extends Handler {
public MyHandler() {
}
private Context context;
public MyHandler(Context context) {
super( );
this.context = context;
}
private List<Course> mList = new ArrayList<>( );
@Override
public void handleMessage(Message msg) {
switch (msg.what) { //根据标志执行不同操作
case 1:
//Log.e("....",msg.obj.toString());
break;
case 2:
mList.add((Course) msg.obj);
// Log.e("....",((Course)msg.obj).getId());
break;
}
}
}
11.使用了bomb后端云
一个坑:不要传ImageView类型的,否则不起作用。它有固定的类型,详情使用等等看官方文档bmob官方文档
第二个坑:包冲突okio
解决:先按shift shift 输入 okio 查找到所用okio出现的包
然后再(app)gradle里这样写
implementation ('com.squareup.okhttp3:okhttp:3.4.1'){
exclude group: 'com.squareup.okio', module: 'okio'
}
我这里是bmob的SDK与okhttp里面okio冲突了,错误信息因为当时没有及时整理所以没有了。
12.ActivityCollector
方便管理activity,可以随时随地的结束
public class ActivityCollector {
public static List<Activity> activities = new ArrayList<>();
public static void addActivity(Activity activity){
activities.add(activity);
}
public static void removeActivity(Activity activity){
activities.remove(activity);
}
public static void finishAll(){
for(Activity activity : activities){
if(!activity.isFinishing()){
activity.finish();
}
}
activities.clear();
}
}
在新Activity 的 onCreat方法中 ActivityCollector.addActivity(它自己)
onDestroy方法中 removeActivity(this)