@ TOC
【43.保留小數】
方法一、
//如果用Android包下的則API>24
double aa = 2.3565666;
BigDecimal bigDecimal = new BigDecimal(aa);
//常量值自己去試試
System.out.println("= " + bigDecimal.setScale(2, BigDecimal.ROUND_DOWN));
//ROUND_DOWN--直接删除多餘的小數位
//ROUND_UP--臨近位非零,則直接進位;臨近位為零,不進位
//ROUND_HALF_UP--四舍五入
//ROUND_HALF_DOWN--四舍五入
//注意傳回值
double bg = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println("bg = " + bg);
方式二:(舍入方式)
java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
df.format(你要格式化的數字);
方式三:(格式化)
System.out.println("String.format = " + String.format("%.2f", aa));
方式四:(用的此)
NumberFormat ddf = NumberFormat.getNumberInstance();
ddf.setMaximumFractionDigits(2);
//去掉逗号
ddf.setGroupingUsed(false);
System.out.println("ddf = " + ddf.format(aa));
【44.實體類copy】
隻能是相同實體類
https://www.jianshu.com/p/7034e9a8c2dfpublic class CloneObjectUtils {
public static <T> T cloneObject(T obj) {
T result = null;
ByteArrayOutputStream byteArrayOutputStream = null;
ByteArrayInputStream byteArrayInputStream = null;
ObjectOutputStream outputStream = null;
ObjectInputStream inputStream = null;
try {
//對象寫到記憶體中
byteArrayOutputStream = new ByteArrayOutputStream();
outputStream = new ObjectOutputStream(byteArrayOutputStream);
outputStream.writeObject(obj);
//從記憶體中再讀出來
byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
inputStream = new ObjectInputStream(byteArrayInputStream);
result = (T) inputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null)
outputStream.close();
if (inputStream != null)
inputStream.close();
if (byteArrayOutputStream != null)
byteArrayOutputStream.close();
if (byteArrayInputStream != null)
byteArrayInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}
Java中的 Android中的 【45.一些實用方法】
有5個Boolean值,當全部為true或者為false時為true,隻要有一個不一樣就為false??
//(異或^)這裡是當所有相同時傳回true,有一個不同傳回false
public static boolean checkSameBoolean(List<Boolean> list) {
int m = 0;
for (int i = 0; i < list.size(); i++) {
Boolean aBoolean = list.get(i);
if (aBoolean) {
m++;
} else {
m--;
}
}
return Math.abs(m) == list.size();
}
//方法二、未驗證
public static boolean checkSameBoolean2(List<Boolean> list) {
if (list == null || list.size() == 0) return false;
Boolean aBoolean = list.get(0);
for (int i = 1; i < list.size(); i++) {
if (aBoolean != list.get(i)) {
return false;
}
}
return true;
}
//kotlin
fun checkSameBoolean(list:MutableList<Boolean>):Boolean{
if(list.isNullOrEmpty()) return false;
val first=list[0]
list.filterNot{ it == first }.forEach { _->return false}
return true
}
判斷是否有相同值
//判斷數組中是否有相同值 方法一、
int[] ar = new int[]{1, 2, 1};
for (int i = 0; i < ar.length; i++) {
for (int j = i + 1; j < ar.length; j++) {
if (ar[i] == ar[j]) {
System.out.println("true = " + true);
}
}
}
//方法二、
int[] ar = new int[]{1, 2, 3};
Set set = new HashSet();
for (int i = 0; i < ar.length; i++) {
set.add(ar[i]);
}
System.out.println("set = " + set.size() + " " + ar.length);
去除重複
//java數組中去掉重複資料可以使用set集合,set本身内部是不儲存重複的資料的
public static void main(String[] args) {
int[] testArr = {5, 5, 1, 2, 3, 6, -7, 8, 6, 45, 5};//建立一個int類型數組
System.out.println(Arrays.toString(testArr));
Set<Integer> set = new TreeSet<Integer>();//建立一個set集合
for (int i : testArr) {
set.add(i);
}
Integer[] arr2 = set.toArray(new Integer[0]);
// 數組的包裝類型不能轉 隻能自己轉;吧Integer轉為為int數組;
int[] result = new int[arr2.length];
for (int i = 0; i < result.length; i++) {
result[i] = arr2[i];
}
System.out.println(Arrays.toString(arr2));
}
【46.添加集合送出資料時】
實體類在for裡面new,list字段在for裡面添加
for(){
A a=new A();
.....
data_list.add(a);
}
【47.Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6】

解決方案:
android {
...
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
看下這裡:
【48.Android 通過接口傳值簡單講解】
[
http://blog.csdn.net/sunshine_mood/article/details/49874039]()
第一步:定義接口類:
public interface Listener {
void send(String s);
}
第二步:傳遞類發送資料:
public class Data {
public Listener mListener;//接口
public Data(Listener mListener) {
this.mListener = mListener;
}
public void sends(){
mListener.send("越努力越成功,越聚貴人!");//開始發送資料
}
}
第三步:接受類接收資料:
public class MainActivity extends Activity implements Listener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Data data = new Data(this);//執行個體化data類
data.sends();//啟動發送
}
@Override
public void send(String s) {
Log.e("tag: ",s);
}
}
【49.有規律的顯示時間】
http://blog.csdn.net/sunshine_mood/article/details/49873809private long currentTime;//目前系統時間
private long oldTime;//過去時間
//以七秒為周期顯示時間
public String getTimes(){
currentTime = System.currentTimeMillis();//獲得目前時間
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");//格式化日期
Date data = new Date();
String str = sdf.format(data);
if(currentTime-oldTime>=7*1000){//每隔7秒鐘傳回一下時間
oldTime = currentTime;
return str;
}else{
return "";
}
}
【50.系統AlertDialog點選确定彈窗不消失】
1.種
final AlertDialog mDialog=new AlertDialog.Builder(this).setPositiveButton("确定", null).setNegativeButton("取消", null).create();
mDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button positionButton=mDialog.getButton(AlertDialog.BUTTON_POSITIVE);
Button negativeButton=mDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
positionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"确定",Toast.LENGTH_SHORT).show();
mDialog.dismiss();
}
});
negativeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_SHORT).show();
}
});
}
});
mDialog.show();
原文連結:https://blog.csdn.net/wanglaohushiwo/article/details/54316616
2.種
final AlertDialog dialog = new AlertDialog.Builder(mContext).setView(view)
.setPositiveButton("确定", null)
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
//這裡必須要先調show()方法,後面的getButton才有效
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(et.getText())) {
showToast("文字不能為空");
return;
}
dialog.dismiss();
}
});
【51.自定義Dialog(系統)】
https://www.cnblogs.com/gzdaijie/p/5222191.html<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ffff"
android:gravity="center"
android:padding="10dp"
android:text="Dialog标題"
android:textSize="18sp" />
<EditText
android:id="@+id/dialog_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入内容"
android:minLines="2"
android:textScaleX="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00ffff"
android:text="取消" />
<View
android:layout_width="1dp"
android:layout_height="40dp"
android:background="#D1D1D1"/>
<Button
android:id="@+id/btn_comfirm"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00ffff"
android:text="确定" />
</LinearLayout>
</LinearLayout>
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View dialogView = View.inflate(this, R.layout.custom_dialog, null);
//ViewGroup viewGroup = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.dialog_change_product_time, null);
//viewGroup.setOnTouchListener((v, event) -> true);//觸摸容器不dismiss
TextView title = dialogView.findViewById(R.id.title);
EditText input_edt = dialogView.findViewById(R.id.dialog_edit);//輸入内容
Button btn_cancel = dialogView.findViewById(R.id.btn_cancel);//取消按鈕
Button btn_comfirm = dialogView.findViewById(R.id.btn_comfirm);//确定按鈕
builder.setView(dialogView);
builder.setCancelable(true);
AlertDialog dialog = builder.create();
dialog.show();
【52.AndroidStudio連不上夜神模拟器】
https://www.cnblogs.com/yoyoketang/p/9024620.html意思是模拟器adb版本36,SDK的adb版本41,對不上導緻的。複制替換即可
替換adb版本
- 1.檢視目前android-sdk的adb版本号,cmd打開輸入adb
- 2.檢視夜神模拟器(nox)的adb版本号,找到安裝的路徑:\Nox\bin,裡面有個nox_adb.exe,其實就是adb.exe,為了避免沖突在nox裡面換了個名稱。在位址欄左上角輸入cmd
- 3.然後在cmd參考輸入nox_adb,就可以檢視nox裡面adb版本号了
- 4.找到版本号不一樣原因了,接下來把android-sdk裡面的adb.exe版本複制出來,然後改個名稱叫nox_adb.exe,替換nox安裝的路徑:\Nox\bin下的nox_adb.exe檔案就行了
- 5.接下來關掉夜神模拟器,重新開機模拟器,在cmd輸入adb devices就可以了
adb
adb devices //列出所有裝置
adb kill-server //殺死所有adb服務
adb start-server //重新開機adb服務
gradle dependencies
cd .. //傳回上級目錄
【53.View.inflate 與 LayoutInflate.inflate差別】
https://blog.csdn.net/chenliguan/article/details/82314122- View.inflate:沒有将item的布局檔案最外層的所有layout屬性設定
- LayoutInflate.inflate:将item的布局檔案最外層的所有layout屬性設定(有邊框)
@Override
public ViewHolder onCreateViewHolder_(ViewGroup parent, int viewType) {
View itemView = View.inflate(mContext, R.layout.item_consume_recharge_record, null);
return new ConsumeRechargeRecordViewHolder(itemView);
}
@Override
public ViewHolder onCreateViewHolder_(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_consume_recharge_record, parent, false);
return new ConsumeRechargeRecordViewHolder(itemView);
}
【54.Android studio引入aar包】
- 第一步:将aar檔案拷貝到libs目錄下 libs與src同級
- 第二步:修改Module的build.gradle 配置檔案:
- 第三步:
修改dependencies:
添加一行:并重新rebuild
implementation (name:'usericonchooserutil', ext:'aar')
【多元件模式】[
https://www.jianshu.com/p/dfaa2326c741提示:Failed to resolve 的坑~
其它所有直接或間接依賴該library module的module中都應 聲明該aar檔案所在libs目錄的相對路徑 或在project的build.gradle檔案中進行統一配置聲明。否則會同步依賴失敗,提示 Failed to resolve
- 1.将aar檔案拷入想要依賴的library module下的libs檔案夾中;
- 2.在module或project中聲明aar檔案路徑:
方式一、在module中聲明:
在依賴aar檔案的library module的build.gradle檔案中配置如下:
repositories {
flatDir {
dirs 'libs'
}
}
在其它所有直接或間接依賴該library module的module中配置build.gradle檔案如下:
repositories {
flatDir {
dirs '../xxx/libs','libs' //将xxx替換為引入aar檔案的module名
}
}
方式二、在project的build.gradle檔案中統一配置如下:
allprojects {
repositories
flatDir {
dirs project(':xxx').file('libs') //将xxx替換為引入aar檔案的module名
}
}
}
3.在依賴aar檔案的library module下的build.gradle檔案中添加依賴如下:
implementation(name: 'xxx', ext: 'aar') //隻允許在依賴aar檔案的module下調用該aar檔案
或
api (name:'xxx',ext:'aar') //在其他依賴該library module的module中,也可調用該library module所依賴的aar檔案
【55.檢視布局視圖】
方法一、然後打開要抓取的App即可檢視布局視圖
方法二、AndroidStudio自帶
在 Tools ---> Layout Inspector