Android 开发中一些常见问题的总结汇总
最近刚刚换了一份工作还不是太忙,正好把这半年开发Android中遇到的一些问题,汇总整理一下。
1.Android中点击方法:
尽量使用setonclick,最好不要使用onclick,防止方法无法识别,从而导致系统奔溃
2.Android中在提交保存数据时:
要判断有无空格、换行等特殊字符。
3.Android常用的布局
3.1文本+输入框+按钮(图片)在同一行,因为总结的有点晚了,所以效果图已经找不到了,布局如下:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="36dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_marginTop="1dp"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="开始日期:"
android:layout_gravity="center_vertical"
android:textSize="16sp"
android:id="@+id/textViewDialog_start"
/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/editTextDialog_start"
android:enabled="false"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:textSize="16sp"
android:hint="请选择开始日期"
android:background="@color/white"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:src="@mipmap/calendar"
android:id="@+id/imageButton_start" />
</LinearLayout>
3.2常见的view布局如下图:
布局为:
<LinearLayout
android:id="@+id/ll_root_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ripple_item_clicked"
android:padding="10dp">
<ImageView
android:id="@+id/iv_item_pull_icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:scaleType="fitCenter"
android:src="@mipmap/tasklist"/>
<TextView
android:id="@+id/tv_item_pull_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@id/iv_item_pull_icon"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/iv_item_pull_icon"
android:ellipsize="end"
android:gravity="top|start"
android:lineSpacingExtra="2dp"
android:maxLines="2"
android:paddingBottom="6dp"
android:text="标题"
android:textColor="@color/font_black_0"
android:textSize="15sp"/>
<TextView
android:id="@+id/tv_item_pull_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_item_pull_name"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/iv_item_pull_icon"
android:text="排名:23"
android:textColor="@color/font_black_5"
android:textSize="12sp"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/font_black_5"/>
</LinearLayout>
4.Android中editview搜索时键盘取消
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// 先隐藏键盘
((InputMethodManager) editText.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
// 搜索,进行自己要的操作...
call_search(v);
return true;
}
return false;
}
});
5.Android中图片加载
ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(CallCardActivity.this));
String str = cardPersonModel.getRows().get(0).getPhotoURL();
if (str.length() > 2){
String strings = cardPersonModel.getRows().get(0).getPhotoURL().substring(2, str.length());
String string = String.format("%s%s",NetConfig.CARDMAIN_URL,strings);
ImageLoader.getInstance().displayImage(string, imageViewHead);
}
6.Android系统退出弹出提示框
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK )
{
AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
builder.setTitle("系统提示");
builder.setMessage("确定要退出吗?");
builder.setPositiveButton("确定", listener);
builder.setNegativeButton("取消",listener);
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
return false;
}
/**监听对话框里面的button点击事件*/
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
switch (which)
{
case AlertDialog.BUTTON_POSITIVE:// "确认"按钮退出程序
finish();
break;
case AlertDialog.BUTTON_NEGATIVE:// "取消"第二个按钮取消对话框
break;
default:
break;
}
}
};
7.Android全局网络判断:
try {
ConnectivityManager manger = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manger.getActiveNetworkInfo();
//return (info!=null && info.isConnected());//
if(info != null){
}else{
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context);
builder.setTitle("信息提示:");
builder.setMessage("没有可用的网络!");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
android.support.v7.app.AlertDialog alertDialog = builder.create();
alertDialog.show();
}
} catch (Exception e) {
}
5.Android Studio 出现 Gradle's dependency cache may be corrupt 错误分析
gradle-wrapper.properties里修改了gradle的版本,与之前没有报错的gradle版本一致。就可以了
6.在Android studio中导入eclipse项目时,可以通过手动的更改gradle版本(如上5所述)和build.gradle中的版本号来加快导入的速度