android开发 代码模板

2023-10-10 16:11:23 浏览数 (1)

模板

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

1、OKHttp发送post请求模板

代码语言:javascript复制
	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模板

代码语言:javascript复制
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消息弹出模板

代码语言:javascript复制
Toast.makeText(this,“内容”,Toast.LENGTH_SHORT).show();

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

4. 收起软键盘

方式一:

代码语言:javascript复制
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0) ;

方式二(推荐):

代码语言:javascript复制
InputMethodManager imm =  (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
if(imm != null) { 
		imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(),0); 
		    		 }

5.适配自定义字体

代码语言:javascript复制
//修改字体
Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/ziti.ttf");
// 应用字体
textView.setTypeface(typeFace);

6.toolbar模板

代码语言:javascript复制
public void setToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("");

    }

7、sharePreference读取和编辑

读取
代码语言:javascript复制
SharedPreferences sp = getSharedPreferences("base", MODE_PRIVATE);
string=sp.getString( , )
编辑
代码语言:javascript复制
SharedPreferences.Editor editor = getSharedPreferences("base", MODE_PRIVATE).edit();
editor.putString();
editor.commit();

8、PagerAdapter模板

代码语言:javascript复制
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接受消息

代码语言:javascript复制
Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            Bundle data = msg.getData();
        	
            return false;
        }
    });

10.创建新线程,http请求

代码语言:javascript复制
 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、动画模板

代码语言:javascript复制
//第一个参数我设置的是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();
    }

0 人点赞