关于Android的一些杂项

来源:互联网 发布:sql数据库管理工具 编辑:程序博客网 时间:2024/06/07 19:00

最近做完了一个项目,摘取一下其中一些零碎的知识,以后用于拓展

1.双击返回键退出

private static Boolean isExit = false;    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        if(keyCode == KeyEvent.KEYCODE_BACK)        {            exitBy2Click(); //调用双击退出函数        }        return false;    }    private void exitBy2Click() {        Timer tExit = null;        if (isExit == false) {            isExit = true; // 准备退出            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();            tExit = new Timer();            tExit.schedule(new TimerTask() {                @Override                public void run() {                    isExit = false; // 取消退出                }            }, 2000); // 如果2秒钟内没有按下返回键,则启动定时器取消掉刚才执行的任务        } else {            finish();        }    }

2.运用正则判断是否符合格式(这和普通的Java一样)

  /**     *     * @param email     * @return 判断Email是否符合格式     */    private boolean judgeMail(String email){        String regEx="^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";        Pattern p=Pattern.compile(regEx);        Matcher m=p.matcher(email);        if (m.find()){            return true;        }        else {            return false;        }    }

3.PopupWindow

PopupWindow mPopWindow; private void showPopupWindow() {        View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popupdropmenu, null);        mPopWindow = new PopupWindow(contentView);        mPopWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);        mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);        mPopWindow.setOutsideTouchable(true);        TextView tv1 = (TextView)contentView.findViewById(R.id.pop_quit);        TextView tv2 = (TextView)contentView.findViewById(R.id.pop_switch);        TextView tv3 = (TextView)contentView.findViewById(R.id.pop_info);        TextView tv4 =(TextView)contentView.findViewById(R.id.pop_set_info);        tv1.setOnClickListener(this);        tv2.setOnClickListener(this);        tv3.setOnClickListener(this);        tv4.setOnClickListener(this);        mPopWindow.showAsDropDown(btnUserInfo);    }
popupdropmenu.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:background="#ffffff"    android:orientation="vertical"    android:paddingBottom="2dp">    <TextView        android:id="@+id/pop_quit"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:textColor="#666666"        android:textSize="20dp"        android:text="退出"/>    <View        android:layout_width="match_parent"        android:layout_height="4dp"        android:background="#f2f2f2"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20dp"        android:id="@+id/pop_switch"        android:textColor="#666666"        android:text="切换账号"/>    <View        android:layout_width="match_parent"        android:layout_height="4dp"        android:background="#f2f2f2"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20dp"        android:textColor="#666666"        android:id="@+id/pop_info"        android:text="个人信息"/>    <View        android:layout_width="match_parent"        android:layout_height="4dp"        android:background="#f2f2f2"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20dp"        android:textColor="#666666"        android:id="@+id/pop_set_info"        android:text="修改信息"/>    <View        android:layout_width="match_parent"        android:layout_height="4dp"        android:background="#f2f2f2"/></LinearLayout>

4.Spinner

int ReviewTimeArray[]={0,1,2,3,4,5,6,7,15,30};Spinner spinner;spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {            @Override            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {                reviewTime=ReviewTimeArray[position];            }            @Override            public void onNothingSelected(AdapterView<?> parent) {            }        });
xml

<Spinner        android:layout_width="152dp"        android:layout_height="32dp"        android:layout_gravity="center"        android:entries="@array/review_time"        android:spinnerMode="dropdown"        android:id="@+id/sp_review"        android:background="@drawable/box_pull_select">    </Spinner>
array
<?xml version="1.0" encoding="utf-8"?><resources>    <string-array name="review_time">        <item>系统默认</item>        <item>1天</item>        <item>2天 </item>        <item>3天</item>        <item>4天</item>        <item>5天</item>        <item>6天</item>        <item>7天</item>        <item>15天</item>        <item>30天</item></string-array></resources>

4.检测TextView状态

EditText text= (EditText) findViewById(R.id.tv_test);        text.addTextChangedListener(new TextWatcher() {            @Override            public void beforeTextChanged(CharSequence s, int start, int count, int after) {            }            @Override            public void onTextChanged(CharSequence s, int start, int before, int count) {            }            @Override            public void afterTextChanged(Editable s) {            }        });
借用这个ChangedListener在项目中我成功监听了一个RecycleView内部Item的Editext内容

原创粉丝点击