android开发 代码模板

模板

模板更新中········

1、OKHttp发送post请求模板

	String value1,value2
	String url = "";
	OkHttpClient okHttpClient = new OkHttpClient();
	RequestBody formBody = new FormBody.Builder()//填充数据
                            .add("key1", value1)
                            .add("key2", value1)
                            .build();
	final Request request = new Request.Builder()
                            .url(url)
                            .post(formBody)
                            .build();
	Call call = okHttpClient.newCall(request);
	call.enqueue(new Callback() {
		@Override
		public void onFailure(Call call, IOException e) {
                            Log.e("error", "onFailure: ", e);
                        }

		@Override
		public void onResponse(Call call, Response response) throws IOException {
                            Log.i("info", "onResponse: " + response.body().string());
                        }
                    });

2、AlertDialog模板

AlertDialog alertDialog = new AlertDialog.Builder(v.getContext()).create();
alertDialog.setTitle("");
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.setMessage("");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "", new DialogInterface.OnClickListener() {
	@Override
	public void onClick(DialogInterface dialog, int which) {
                           
                        }
                    });
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "退", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                          
                        }
                    });
//alertDialog0.setCanceledOnTouchOutside(false); //此行可以让警告框的外部区域不可触摸
alertDialog.show();

3. Toast消息弹出模板

Toast.makeText(this,“内容”,Toast.LENGTH_SHORT).show();

有的时候会报错,使用下面的代码:
Looper.prepare();
Toast.makeText(getApplicationContext(),“内容”,Toast.LENGTH_SHORT);
Looper.loop();

4. 收起软键盘

方式一:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0) ;

方式二(推荐):

InputMethodManager imm =  (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
if(imm != null) { 
		imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(),0); 
		    		 }

5.适配自定义字体

//修改字体
Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/ziti.ttf");
// 应用字体
textView.setTypeface(typeFace);

6.toolbar模板

public void setToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("");

    }

7、sharePreference读取和编辑

读取

SharedPreferences sp = getSharedPreferences("base", MODE_PRIVATE);
string=sp.getString( , )

编辑

SharedPreferences.Editor editor = getSharedPreferences("base", MODE_PRIVATE).edit();
editor.putString();
editor.commit();

8、PagerAdapter模板

class StudyWordAdapter extends PagerAdapter {
    public List<View> list_view;
    private TextView word, translate;

    @Override
    public int getCount() {
        return list_view.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        container.addView(list_view.get(position));
        return list_view.get(position);
    }


    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View)object);
    }
    @Override
    public int getItemPosition(Object object)
    {
        return null!=list_view&& list_view.size()==0?POSITION_NONE:super.getItemPosition(object);
    }
}

9.Handler接受消息

Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            Bundle data = msg.getData();
        	
            return false;
        }
    });

10.创建新线程,http请求

 Runnable networkTask = new Runnable() {
            @Override
            public void run() {
                // TODO
                // 在这里进行 http request.网络请求相关操作
                Message msg = new Message();
                Bundle data = new Bundle();
                msg.setData(data);
                handler.sendMessage(msg);
            }
        };
        new Thread(networkTask).start();

11、动画模板

//第一个参数我设置的是TextView,根据需要改变。
public void setAnimation(TextView textView,int b) {
        //动画
        ValueAnimator anim = ValueAnimator.ofInt(0, 360);
        anim.setDuration(250);//持续时间
        anim.setRepeatCount(0);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {

                int currentValue = (Integer) animation.getAnimatedValue();
                // 获得改变后的值
                      ((ConstraintLayout.LayoutParams)textView.getLayoutParams()). = currentValue;

            }
        });

        anim.start();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值