- 隐藏键盘
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); } }
- 判断EditText文本是否为空:
使用TextUtils.isEmpty()方法
源代码:
public static boolean More ...isEmpty(CharSequence str) { if (str == null || str.length() == 0) return true; else return false; }
- EditText提示错误信息:
mEdit.setError(getString(R.string.enter_address));
获取焦点:
mEdit.requestFocus();
- EditText 自动提示:
- Android当前版本号获取:Build.VERSION_SDK_INT
- AsyncTask的启动方式,待续
- 一个简单的管理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(); } }
- 打勾显示输入密码:
有时需要打勾时显示输入的密码:
checkbox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { editext.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); } else { editext.setTransformationMethod(PasswordTransformationMethod.getInstance()); } } });
- Activity中异步任务的bug:之前的文中有提,如果要启动一个异步任务,应该用一个Fragment去封装,避免转屏后Activity销毁,异步任务返回,操作原来的Activity界面,导致程序奔溃。具体见:
- 除了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 } } }
- android.R.id.home 按返回键时触发
public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home:
- 异步任务取消:
if (task != null && task.getStatus() != AsyncTask.Status.FINISHED) { task.cancel(mayInterruptIfRunning); }
杂
最新推荐文章于 2024-09-11 15:11:34 发布