zoukankan      html  css  js  c++  java
  • android studio开发小笔记1

    1、点击空白处隐藏软键盘

     /*
       隐藏软键盘
       * */
        public boolean dispatchTouchEvent(MotionEvent ev) {
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                View v = getCurrentFocus();
                if (isShouldHideInput(v, ev)) {
    
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    }
                }
                return super.dispatchTouchEvent(ev);
            }
    
            if (getWindow().superDispatchTouchEvent(ev)) {
                return true;
            }
            return onTouchEvent(ev);
        }
    
    
        public  boolean isShouldHideInput(View v, MotionEvent event) {
            if (v != null && (v instanceof EditText)) {
                int[] leftTop = { 0, 0 };
    
                v.getLocationInWindow(leftTop);
                int left = leftTop[0];
                int top = leftTop[1];
                int bottom = top + v.getHeight();
                int right = left + v.getWidth();
                if (event.getX() > left && event.getX() < right
                        && event.getY() > top && event.getY() < bottom) {
    
                } else {
                    return true;
                }
            }
            return false;
        }
    View Code

    2、沉浸式状态栏

            //设置沉浸式状态栏 android 4.4 及以上支持
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                //透明状态栏
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                //透明导航栏
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            }
    View Code

    3、设置导航栏

    xml代码:

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:background="#5caef9"
            android:minHeight="50dp"
            android:gravity="center"
            android:textAlignment="center"
            >
        </android.support.v7.widget.Toolbar>
    View Code

    后台代码:

            //设置导航栏
          Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar1);//鑾峰彇椤甸潰鐨勫伐鍏锋爮
    
            toolbar.setTitle("设置服务");
            toolbar.setTitleMarginStart(370);
            toolbar.setTitleTextColor(Color.WHITE);
            toolbar.setTitleMarginTop(10);
            toolbar.setNavigationIcon(R.mipmap.allback);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(SettingServiceActivity.this,MyselfLoginActivity.class);
                    startActivity(intent);
                }
            });
    View Code

    4、获取屏幕宽高和设置控件宽高

            //设置输入框宽高
            EditText tvservicepath = (EditText)findViewById(R.id.tvservicepath);
            tvservicepath.setText(R.string.serverpath);//获取资源文件
            //获取屏幕高宽
            WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics dm = new DisplayMetrics();
            wm.getDefaultDisplay().getMetrics(dm);
            int width = dm.widthPixels;         // 屏幕宽度(像素)
            int height = dm.heightPixels;       // 屏幕高度(像素)
            float density = dm.density;         // 屏幕密度(0.75 / 1.0 / 1.5)
            int densityDpi = dm.densityDpi;     // 屏幕密度dpi(120 / 160 / 240)
            // 屏幕宽度算法:屏幕宽度(像素)/屏幕密度
            int screenWidth = (int) (width / density);  // 屏幕宽度(dp)
            int screenHeight = (int) (height / density);// 屏幕高度(dp)
    
            ViewGroup.LayoutParams para;
            para = tvservicepath.getLayoutParams();
            para.height = screenHeight/2;
            para.width = width-50;
            tvservicepath.setLayoutParams(para);
    View Code

    5、在手机本地保存数据和读取(参考:https://blog.csdn.net/acm_th/article/details/50009575)

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.savedatademo.MainActivity" >
    
        <EditText
            android:id="@+id/input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入" />
    
        <Button
            android:id="@+id/btn1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/input"
            android:text="保存数据" />
    
        <Button 
            android:id="@+id/btn2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btn1"
            android:text="读取数据"/>
    
    </RelativeLayout>
    View Code
    package com.example.savedatademo;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintStream;
    import java.util.Scanner;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Window;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        private Button saveBtn = null;
        private Button readBtn = null;
        private EditText input = null;
        private final static String FILE_NAME = "xth.txt"; // 设置文件的名称
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_main);
    
            input = (EditText) findViewById(R.id.input);
            saveBtn = (Button) findViewById(R.id.btn1);
            readBtn = (Button) findViewById(R.id.btn2);
    
            saveBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    String data = input.getText().toString();
                    save(data);
                    Toast.makeText(MainActivity.this, "保存数据成功!", Toast.LENGTH_SHORT)
                            .show();
                }
            });
            readBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    StringBuffer sb = read();
                    Toast.makeText(MainActivity.this, "读取数据为:" + sb,
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
    
        private StringBuffer read() {
            FileInputStream in = null;
            Scanner s = null;
            StringBuffer sb = new StringBuffer();
            try {
                in = super.openFileInput(FILE_NAME);
                s = new Scanner(in);
                while (s.hasNext()) {
                    sb.append(s.next());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return sb;
        }
    
        private void save(String data) {
            FileOutputStream out = null;
            PrintStream ps = null;
            try {
                out = super.openFileOutput(FILE_NAME, Activity.MODE_APPEND);
                ps = new PrintStream(out);
                ps.println(data);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (out != null) {
                    try {
                        out.close();
                        ps.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    }
    View Code

    这是追加内容的,如果覆盖,改成:

    out = super.openFileOutput(FILE_NAME, Activity.MODE_WORLD_WRITEABLE);

     6、代码设置按钮被点击使用performClick()方法。

    7、设置控件的显示与隐藏:

    View缺省为可见的,在应用可以通过setVisibility 修改View的可见性,

    View.VISIBLE View可见,View.INVISIBLE View不可以见,但仍然占据可见时的大小和位置。

    View.GONE View不可见,且不占据空间。

  • 相关阅读:
    java集合源码
    数据库表链接的几种方式
    面试题(RabbitMQ)
    常见面试题(Redis)
    某奥笔试题
    Servlet
    1——Django的基础及环境搭建
    6.13---example
    6.12---知道参数的重要性------插入数据-删除数据-修改数据注意Map
    6.12---前提两个对象的成员必须一致,才能将有数据的对象将数据传给反射获取的对象conver(有数据对象,目标对象)
  • 原文地址:https://www.cnblogs.com/luo1240465012/p/9275263.html
Copyright © 2011-2022 走看看