1. 隐藏键盘

     

    private void hideKeyboard() {
            InputMethodManager localInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                localInputMethodManager.hideSoftInputFromWindow(currentFocus.getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }


    1.  判断EditText文本是否为空:

    使用TextUtils.isEmpty()方法

    源代码:

    public static boolean More ...isEmpty(CharSequence str) {
        if (str == null || str.length() == 0)
           return true;
       else
           return false;
    }


     

    1. EditText提示错误信息:

    mEdit.setError(getString(R.string.enter_address));

    获取焦点:

    mEdit.requestFocus();

    1. EditText 自动提示:

     

    1. Android当前版本号获取:Build.VERSION_SDK_INT

     

    1. AsyncTask的启动方式,待续

     

    1. 一个简单的管理AlertDialog DialogFragment

     

    public class MyDialogFragment extends DialogFragment {
    public final static String TAG = "DialogFragment";
     
    // Argument bundle keys
    private final static String BUNDLE_KEY = "DialogFragmentData";
     
    // Public no-args constructor needed for fragment re-instantiation
    public DuplicateAccountDialogFragment() {
    }
     
    /**
     * Create the dialog with parameters
     */
    public static DuplicateAccountDialogFragment newInstance(String note) {
    DuplicateAccountDialogFragment f = new DuplicateAccountDialogFragment();
    Bundle b = new Bundle();
    b.putString(BUNDLE_KEY, note);
    f.setArguments(b);
    return f;
    }
     
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    Context context = getActivity();
    String accountName = getArguments().getString(BUNDLE_KEY);
     
    return new AlertDialog.Builder(context)
    .setTitle("title")
    .setMessage("message")
    .setPositiveButton(R.string.okay_action,
    new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog,
    int which) {
    }
    }).create();
    }
    }


     

    1. 打勾显示输入密码:

    有时需要打勾时显示输入的密码:

    checkbox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()
            {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                {
                    if (isChecked)
                    {
                        editext.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    } else {
                        editext.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    }
                }
            });


    1. Activity中异步任务的bug:之前的文中有提,如果要启动一个异步任务,应该用一个Fragment去封装,避免转屏后Activity销毁,异步任务返回,操作原来的Activity界面,导致程序奔溃。具体见:


     

    1. 除了onClickListener()还有setOnTouchListener() 不同对于同一个控件同时使用这两个方法有可能造成冲突,具体解决以后再记。

     

    @Override
    public boolean onTouch(View v, MotionEvent event) {
    if (v.getId() == R.id.view_password) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
    //Todo something
    }
    }
    }


     

    1. android.R.id.home 按返回键时触发

    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:


    1. 异步任务取消:

    if (task != null && task.getStatus() != AsyncTask.Status.FINISHED) {
    task.cancel(mayInterruptIfRunning);
    }

     

     

     

     

     

     

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值