Android Studio 4.1.2 编译 老项目 出现Gradle sync failed: Unsupported method: SyncIssue.getMultiLineMessage

Gradle sync failed: Unsupported method: SyncIssue.getMultiLineMessage().
The version of Gradle you connect to does not support that method.
To resolve the problem you can change/upgrade the target version of Gradle you connect to.
Alternatively, you can ignore this exception and read other information from the model.
Consult IDE log for more details (Help | Show Log) (31 s 894 ms)

问题原因:Android Studio 4.1.2 编译老项目无法通过,主要原因是Gradle版本和Gradle Plugin插件版本问题。 阅读全文

Android Fragment RelativeLayout 动态生成TextView 事件

package cn.mf.cts;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * CopyRright (c)2018-2028: chanpinxue.cn
 * Project: cts
 * Module Name: Mine
 * Comments: 我的
 * JDK version used: JDK1.8
 * Author: jzh
 * Create Date: 2018-09-17
 * Modified By: jzh
 * Modified Date: 2018-09-17
 * Why & What is modified:
 * Version: <1.0>
 */
public class MineFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, null,false);


        // TextView宽度和高度
        int width = 200;
        int height = 120;

        // 自定义layout组件
        RelativeLayout layout = new RelativeLayout(getContext());
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);

        // 创建16个TextView,每行放置4个TextView
        TextView arrTV[] = new TextView[16];
        int j = -1;
        for (int i = 0; i <= 15; i++) {
            arrTV[i] = new Button(getContext());
            arrTV[i].setId(100 + i);
            arrTV[i].setText("测试" + i);
            arrTV[i].setTextColor(Color.RED);
            arrTV[i].setTextSize(12);
            RelativeLayout.LayoutParams btParams = new RelativeLayout.LayoutParams(width, height);
            if (i % 4 == 0) {
                j++;
            }
            btParams.leftMargin = 10 + (width + 10) * (i % 4); // 横坐标定位
            btParams.topMargin = 20 + height * j; // 纵坐标定位
            layout.addView(arrTV[i], btParams); // 加入layout组件
        }

        // 加入view
        //this.setContentView(layout);
        getActivity().addContentView(layout, layoutParams);

        // 批量设置监听
        for (int k = 0; k <= arrTV.length - 1; k++) {
            arrTV[k].setTag(k); // 设置标记

            arrTV[k].setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int i = (Integer) v.getTag();
                    System.out.println("测试" + String.valueOf(i));
                }
            });
        }

        return view;
    }
}

Android Notification 状态栏通知

import android.app.Notification;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
import android.app.PendingIntent;        

        // 定义一个PendingIntent,点击Notification后,启动一个Activity。
        Intent it = new Intent(this, LoginActivity.class);
        PendingIntent pit = PendingIntent.getActivity(this, 0, it, 0);

        // 创建通知栏管理工具
        NotificationManager mNManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // 实例化通知栏构造器
        Notification.Builder mBuilder = new Notification.Builder(this);

        // 设置Builder
        // 设置标题
        mBuilder.setContentTitle("我是标题")
                // 设置内容
                .setContentText("我是内容")
                // 设置大图标
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                // 设置小图标
                .setSmallIcon(R.mipmap.ic_launcher_round)
                // 设置通知时间
                .setWhen(System.currentTimeMillis())
                // 设置通知方式:声音、震动、呼吸灯等效果。这里通知方式为声音。
                .setDefaults(Notification.DEFAULT_SOUND)
                // 设置点击后取消Notification
                .setAutoCancel(true)
                // 设置PendingIntent
                .setContentIntent(pit);

        // 发送通知请求
        mNManager.notify(1000, mBuilder.build());

        // 根据ID取消Notification
        //mNManager.cancel(1000);

如果在Fragment里使用,需要适当调整。 阅读全文

Android Fragment ArrayAdapter ListView 自定义列表显示

package cn.mf.cts;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

/**
 * CopyRright (c)2018-2028: chanpinxue.cn
 * Project: cts
 * Module Name: Index
 * Comments: 首页
 * JDK version used: JDK1.8
 * Author: jzh
 * Create Date: 2018-09-17
 * Modified By: jzh
 * Modified Date: 2018-09-17
 * Why & What is modified:
 * Version: <1.0>
 */
public class IndexFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_index, null, false);

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);

        // 基础数据
        String[] arr = {"A","B","C","D","E"};
        // 创建ArrayAdapter
        ArrayAdapter adapter = new ArrayAdapter(getContext(), android.R.layout.simple_expandable_list_item_1, arr);
        // 获取ListView对象,通过调用setAdapter方法为ListView设置Adapter设置适配器
        final ListView list = new ListView(getContext());
        list.setAdapter(adapter);

        // import android.widget.AdapterView.OnItemClickListener;
        list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position,
                                    long id) {
                String text = list.getItemAtPosition(position).toString();
                System.out.println("选择" +text);
            }
        });

        return view;
    }
}

Android Fragment RelativeLayout 动态生成按钮 事件

package cn.mf.cts;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

/**
 * CopyRright (c)2018-2028: chanpinxue.cn
 * Project: cts
 * Module Name: Index
 * Comments: 首页
 * JDK version used: JDK1.8
 * Author: jzh
 * Create Date: 2018-09-17
 * Modified By: jzh
 * Modified Date: 2018-09-17
 * Why & What is modified:
 * Version: <1.0>
 */
public class IndexFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_index, null, false);

        // 按钮宽度和高度
        int width = 200;
        int height = 120;

        // 自定义layout组件
        RelativeLayout layout = new RelativeLayout(getContext());
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);

        // 创建16个按钮,每行放置4个按钮
        Button arrBtn[] = new Button[16];
        int j = -1;
        for (int i = 0; i <= 15; i++) {
            arrBtn[i] = new Button(getContext());
            arrBtn[i].setId(100 + i);
            arrBtn[i].setText("按钮" + i);
            arrBtn[i].setTextColor(Color.RED);
            RelativeLayout.LayoutParams btParams = new RelativeLayout.LayoutParams(width, height);
            if (i % 4 == 0) {
                j++;
            }
            btParams.leftMargin = 10 + (width + 10) * (i % 4); // 横坐标定位
            btParams.topMargin = 20 + height * j; // 纵坐标定位
            layout.addView(arrBtn[i], btParams); // 加入layout组件
        }

        // 加入view
        //this.setContentView(layout);
        getActivity().addContentView(layout, layoutParams);

        // 批量设置监听
        for (int k = 0; k <= arrBtn.length - 1; k++) {
            arrBtn[k].setTag(k); // 设置标记

            arrBtn[k].setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int i = (Integer) v.getTag();
                    System.out.println("按钮" + String.valueOf(i));
                }
            });
        }

        return view;
    }
}
阅读全文

Android 广播 BroadcastReceiver 动态注册

// dynamicReceiver
DynamicReceiver dynamicReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    // 动态广播BroadcastReceiver
    // 实例化IntentFilter对象
    IntentFilter filter = new IntentFilter();
    filter.addAction("cts_bcr");
    dynamicReceiver = new DynamicReceiver();
    // 注册广播接收
    registerReceiver(dynamicReceiver,filter);
}

// 通过继承 BroadcastReceiver建立动态广播接收器
class DynamicReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast toast = Toast.makeText(context,"动态广播:"+ intent.getStringExtra("cts_bcr_msg"), Toast.LENGTH_SHORT);
        // 设置弹出位置
        toast.setGravity(Gravity.TOP,0,0);
        toast.show();
    }
}

// 动态注册需在Acticity生命周期onPause通过unregisterReceiver()方法移除广播接收器,优化内存空间,避免内存溢出
@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(dynamicReceiver);
}

// 发送测试
btnOK.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setAction("cts_bcr");
        intent.putExtra("cts_bcr_msg","cts_bcr_msg 测试");
        sendBroadcast(intent);
    } 
}

Android AlertDialog

import android.app.AlertDialog;
import android.content.DialogInterface;


        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("标题")
                .setMessage("内容")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //
                    }
                })
                .create();
        dialog.show();

Android 数据库 sqlite 操作

测试类

package cn.mf.cts.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * CopyRright (c)2018-2028: chanpinxue.cn
 * Project: cts
 * Module Name: SQLiteDBUtil
 * Comments: SQLite测试类
 * JDK version used: JDK1.8
 * Author: jzh
 * Create Date: 2018-11-23
 * Modified By: jzh
 * Modified Date: 2018-11-23
 * Why & What is modified:
 * Version: <1.0>
 */
public class SQLiteDBUtil {

    private static SQLiteDBUtil dbUtil;
    private SQLiteDatabase db;

    /**
     * 单例模式
     * @return
     */
    public static SQLiteDBUtil getInstance() {
        if (dbUtil == null) {
            dbUtil = new SQLiteDBUtil();
            return dbUtil;
        }
        return dbUtil;
    }

    /**
     * 初始化/建表
     * @param context 上下文对象
     */
    public void init(Context context) {
        String path = context.getCacheDir().getPath() + "/cts.db";
        db = SQLiteDatabase.openOrCreateDatabase(path, null);
        String sql = " create table if not exists app_users "
                   + "(user_id integer primary key autoincrement, "
                   + " user_code text(50), user_name text(50) ) ";
        db.execSQL(sql);
    }

    /**
     * 新增
     */
    public long insert(String usercode, String username) {
        ContentValues cv = new ContentValues();
        cv.put("user_code", usercode);
        cv.put("user_name", username);
        long flag = db.insert("app_users", null, cv);
        return flag;
    }

    /**
     * 修改
     */
    public int update(String usercode, String username) {
        ContentValues cv = new ContentValues();
        cv.put("user_name", username);
        int flag = db.update("app_users", cv, "user_code = ?", new String[]{usercode});
        return flag;
    }

    /**
     * 删除
     */
    public int delete(String usercode) {
        int flag = db.delete("app_users", "user_code = ?", new String[]{usercode});
        return flag;
    }

    /**
     * 查询数据
     */
    public String select(String user_code) {
        String username = "";
        Cursor cursor = db.query("app_users", null, "user_code = ?",  new String[]{user_code}, null, null, null);
        while (cursor.moveToNext()) {
            int userid = cursor.getInt(cursor.getColumnIndex("user_id"));
            username = cursor.getString(cursor.getColumnIndex("user_name"));
            Log.e("sqlite", String.valueOf(userid) + " " + username);
        }
        if (cursor != null) {
            cursor.close();
        }
        return username;
    }

}

调用 阅读全文