android 记录笔记

来源:互联网 发布:ubuntu内核 编辑:程序博客网 时间:2024/06/15 23:45

自己笔记  每天时时刻刻都会更新  看到好就会添加进来


selector选择的器

<?xml version="1.0" encoding="utf-8" ?>   <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 默认时的背景图片-->    <item android:drawable="@drawable/pic1" />    <!-- 没有焦点时的背景图片 -->    <item android:state_window_focused="false"           android:drawable="@drawable/pic1" />   <!-- 非触摸模式下获得焦点并单击时的背景图片 -->    <item android:state_focused="true" android:state_pressed="true"   android:drawable= "@drawable/pic2" /> <!-- 触摸模式下单击时的背景图片-->  <item android:state_focused="false" android:state_pressed="true"   android:drawable="@drawable/pic3" />  <!--选中时的图片背景-->    <item android:state_selected="true"   android:drawable="@drawable/pic4" />   <!--获得焦点时的图片背景-->    <item android:state_focused="true"   android:drawable="@drawable/pic5" />   </selector>

相关属性:

android:state_selected是选中
android:state_focused是获得焦点
android:state_pressed是点击
android:state_enabled是设置是否响应事件,指所有事件



存储日志功能实现:开个线程,实时把打印的信息加入缓存  缓存满了写入IO。不要来一条就进行io操作,io操作比较耗时,

容易卡顿,甚至导致ANR


oncreate和oncreateview的区别

onCreate是指创建该fragment类似于Activity.onCreate,你可以在其中初始化除了view之外的东西,onCreateView是创建该fragment对应的视图,你必须在这里创建自己的视图并返回给调用者,例如

return inflater.inflate(R.layout.fragment_settings, container, false);。

super.onCreateView有没有调用都无所谓,因为super.onCreateView是直接返回null的。


Android Studio的全局查找快捷键:Ctrl+F,全局替换快捷键:Ctrl+R


点击全屏件 webview 从之前的的固定大小转换成全屏大小

 mWebView = (WebView) findViewById(R.id.test_webview);        mWebView.loadUrl("https://v.qq.com/");        mImgs = (ImageView) findViewById(R.id.test_imgs);        mImgss = (ImageView) findViewById(R.id.test_imgss);        mLayoutParams = (FrameLayout.LayoutParams) mWebView.getLayoutParams();        mLayoutParams.height = 400;        mLayoutParams.width = 600 ;        mWebView.setLayoutParams(mLayoutParams);        mImgs.setOnClickListener(new View.OnClickListener() {            private ImageView mImageView;            @Override            public void onClick(View view) {                FrameLayout.LayoutParams mLayoutParams = new FrameLayout.LayoutParams(                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);                mWebView.setLayoutParams(mLayoutParams);                mImgss.setVisibility(View.VISIBLE);            }        });        mImgss.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                mLayoutParams = (FrameLayout.LayoutParams) mWebView.getLayoutParams();                mLayoutParams.height = 400;                mLayoutParams.width = 600 ;                mWebView.setLayoutParams(mLayoutParams);                mImgss.setVisibility(View.GONE);            }        });    }

应用程序在运行的过程中如果需要向手机上保存数据,一般是把数据保存在SDcard中的。
大部分应用是直接在SDCard的根目录下创建一个文件夹,然后把数据保存在该文件夹中。
这样当该应用被卸载后,这些数据还保留在SDCard中,留下了垃圾数据。
如果你想让你的应用被卸载后,与该应用相关的数据也清除掉,该怎么办呢?

通过Context.getExternalFilesDir()方法可以获取到 SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据
通过Context.getExternalCacheDir()方法可以获取到 SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据

如果使用上面的方法,当你的应用在被用户卸载后,SDCard/Android/data/你的应用的包名/ 这个目录下的所有文件都会被删除,不会留下垃圾信息。

而且上面二个目录分别对应 设置->应用->应用详情里面的”清除数据“与”清除缓存“选项



android 模拟点击事件

       
  mButton = (Button) findViewById(R.id.btn);    mButton.setOnClickListener( new View.OnClickListener() {        @Override        public void onClick(View view) {            Toast.makeText(MainActivity.this, "老铁挺住", Toast.LENGTH_LONG).show();        }    });       mc = new MyCount(3000, 1000);       mc.start();   }/*定义一个倒计时的内部类*/class MyCount extends CountDownTimer {    public MyCount(long millisInFuture, long countDownInterval) {        super(millisInFuture, countDownInterval);    }    @Override    public void onTick(long millisUntilFinished) {    }    @Override    public void onFinish() {        mButton.performClick();  //调用 button的点击事件    }}

最简单的Dialog实现

AlertDialog.Builder builder = new AlertDialog.Builder(this);        //先得到构造器        builder.setTitle("提示");                                         //设置标题        builder.setMessage("是否确认退出");       //设置内容        builder.setIcon(R.mipmap.ic_launcher);   //自定义图标        builder.setCancelable(false);           //设置是否能点击,对话框的其他区域取消        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {     //设置其确认按钮和监听事件            @Override            public void onClick(DialogInterface dialog, int which) {                //  which,是哪一个按钮被触发                //      其值如下:                //   Dialog.BUTTON_NEGATIVE     忽略                //   Dialog.BUTTON_POSITIVE     确认                //   Dialog.BUTTON_NEUTRAL      取消                            dialog.dismiss();            }        });        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {     //设置其取消按钮和监听事件            @Override            public void onClick(DialogInterface dialog, int which) {                dialog.dismiss();            }        });        builder.setNegativeButton("忽略", new DialogInterface.OnClickListener() {     //设置其忽略按钮和监听事件            @Override            public void onClick(DialogInterface dialog, int which) {                dialog.dismiss();                 }        });        builder.create();       //创建对话框        builder.show();         //显示对话框    }
 可以利用Which的值,把以上的代码简化:
private void dialog1_1(){        //先new出一个监听器,设置好监听        DialogInterface.OnClickListener dialogOnclicListener=new DialogInterface.OnClickListener(){            @Override            public void onClick(DialogInterface dialog, int which) {                switch(which){                    case Dialog.BUTTON_POSITIVE:                        Toast.makeText(MainActivity.this, "确认" + which, Toast.LENGTH_SHORT).show();                        break;                    case Dialog.BUTTON_NEGATIVE:                        Toast.makeText(MainActivity.this, "取消" + which, Toast.LENGTH_SHORT).show();                        break;                    case Dialog.BUTTON_NEUTRAL:                        Toast.makeText(MainActivity.this, "忽略" + which, Toast.LENGTH_SHORT).show();                        break;                }            }        };        //dialog参数设置        AlertDialog.Builder builder=new AlertDialog.Builder(this);  //先得到构造器        builder.setTitle("提示"); //设置标题        builder.setMessage("是否确认退出?"); //设置内容        builder.setIcon(R.mipmap.ic_launcher);//设置图标,图片id即可        builder.setPositiveButton("确认",dialogOnclicListener);        builder.setNegativeButton("取消", dialogOnclicListener);        builder.setNeutralButton("忽略", dialogOnclicListener);        builder.create().show();    }

android 判断手指滑动方向

//手指按下的点为(x1, y1)手指离开屏幕的点为(x2, y2)    float x1 = 0;    float x2 = 0;    float y1 = 0;    float y2 = 0;  
hideSystemUI();这个方法我自己写的没用  删掉就可以了
@Override     public boolean onTouchEvent(MotionEvent event) {         //继承了Activity的onTouchEvent方法,直接监听点击事件         if (event.getAction() == MotionEvent.ACTION_DOWN) {             //当手指按下的时候             x1 = event.getX();             y1 = event.getY();         }         if (event.getAction() == MotionEvent.ACTION_UP) {             //当手指离开的时候             x2 = event.getX();             y2 = event.getY();             if (y1 - y2 > 50) {                                 Toast.makeText(MainActivity.this, "向上滑", Toast.LENGTH_SHORT).show();             } else if (y2 - y1 > 50) {                 hideSystemUI();                 Toast.makeText(MainActivity.this, "向下滑", Toast.LENGTH_SHORT).show();             } else if (x1 - x2 > 50) {                 hideSystemUI();                 Toast.makeText(MainActivity.this, "向左滑", Toast.LENGTH_SHORT).show();             } else if (x2 - x1 > 50) {                 hideSystemUI();                 Toast.makeText(MainActivity.this, "向右滑", Toast.LENGTH_SHORT).show();             }         }         return super.onTouchEvent(event);     }  


android动态添加布局

//设置界面的布局         mRelativeLayout = new RelativeLayout(this);         setContentView(mRelativeLayout);             //添加一个AbsoluteLayout子布局,并给这个布局添加一个iv         RelativeLayout abslayout = new RelativeLayout(this);         ImageView iv1 = new ImageView(this);         iv1.setImageResource(R.drawable.mo_codescanning_hui);         RelativeLayout.LayoutParams lp0 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,                 ViewGroup.LayoutParams.WRAP_CONTENT);         abslayout.addView(iv1, lp0);           //将这个子布局添加到主布局中         //设置子布局在父容器位置         RelativeLayout.LayoutParams lp_big0 = new RelativeLayout.LayoutParams(                 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);         lp_big0.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.ALIGN_PARENT_LEFT);         mRelativeLayout.addView(abslayout, lp_big0);             //再添加一个子布局  右上btn         RelativeLayout relativeLayout1 = new RelativeLayout(this);         Button btn1 = new Button(this);         btn1.setText("确认");         RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(                 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);         lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.ALIGN_PARENT_RIGHT);         lp1.width = 200;         lp1.setMargins(0,0,20,50);         btn1.setLayoutParams(lp1);         relativeLayout1.addView(btn1, lp1);           //将这个布局添加到主布局中         RelativeLayout.LayoutParams lp_big1 = new RelativeLayout.LayoutParams(                 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);         lp_big1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);         mRelativeLayout.addView(relativeLayout1, lp_big1);                    //再添加一个子布局  左下btn         RelativeLayout relativeLayout2 = new RelativeLayout(this);         Button btn2 = new Button(this);         btn2.setText("手动输入");         btn2.setTextSize(20);         btn2.getBackground().setAlpha(80);         RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(                 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);         lp2.width = 520;         lp2.setMargins(20,30,0,0);         btn2.setLayoutParams(lp2);         relativeLayout2.addView(btn2, lp2);           //将这个布局添加到主布局中         RelativeLayout.LayoutParams lp_big2 = new RelativeLayout.LayoutParams(                 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);         lp_big2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);         mRelativeLayout.addView(relativeLayout2, lp_big2);           //再添加一个子布局 右下btn         RelativeLayout relativeLayout3 = new RelativeLayout(this);         Button btn3 = new Button(this);         btn3.setText("打开照明设备");         btn3.setTextSize(20);         btn3.getBackground().setAlpha(80);           RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(                 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);         lp3.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.ALIGN_PARENT_RIGHT);         lp3.width = 520;         lp3.setMargins(0,30,0,20);         btn3.setLayoutParams(lp3);         relativeLayout3.addView(btn3, lp3);           //将这个布局添加到主布局中         RelativeLayout.LayoutParams lp_big3 = new RelativeLayout.LayoutParams(                 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);         lp_big3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);         mRelativeLayout.addView(relativeLayout3, lp_big3); 

EditText 关闭键盘, 不可输入

       mEditTexts.setInputType(InputType.TYPE_NULL);                mEditTexts.setEnabled(false);

xml中加入一句代码如下:

android:focusableInTouchMode="true"

OkGo 请求数据

   //热点指数        String hostpot_url = "http://v.juhe.cn/toutiao/index?type=top&key=2d222092ba994e3a928d46d83f097c8b";        OkGo.get(hostpot_url)                .execute(new StringCallback() {                    @Override                    public void onSuccess(String s, Call call, Response response) {                        Gson gson = new Gson(); //Gson解析工具                        Hotspot_Bean hotspot_bean = gson.fromJson(s, Hotspot_Bean.class); // 通过gson.from 取出Bean的值                        // 取出Data的集合                        mHotpost_data = hotspot_bean.getResult().getData();                        GridLayoutManager hotpost = new GridLayoutManager(Activity_HomePage.this, 2);                        mLock_hotspot.setLayoutManager(hotpost);                       *//* Hotpost_Adapter hotpost_adapter = new Hotpost_Adapter(Activity_HomePage.this,mHotpost_data);                        mLock_hotspot.setAdapter(hotpost_adapter);*//*                    }                    @Override                    public void onError(Call call, Response response, Exception e) {                        super.onError(call, response, e);                    }                });

URI   URL  URN 的区别

首先,URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源。而URL是uniform resource locator,统一资源定位器,它是一种具体的URI,即URL可以用来标识一个资源,而且还指明了如何locate这个资源。而URN,uniform resource name,统一资源命名,是通过名字来标识资源,比如mailto:java-net@java.sun.com。也就是说,URI是以一种抽象的,高层次概念定义统一资源标识,而URL和URN则是具体的资源标识的方式。URL和URN都是一种URI。



 
原创粉丝点击