专栏名称: AirPython
分享 Python 自动化及爬虫、数据分析实战干货,欢迎关注。
目录
相关文章推荐
新京报书评周刊  ·  梁贵柏与《双药记》:下一个“青蒿素”在哪里? ·  2 天前  
新京报书评周刊  ·  2月,我们选出了18本好书 ·  3 天前  
十点读书  ·  相比申公豹,我更喜欢太乙真人 ·  3 天前  
51好读  ›  专栏  ›  AirPython

微信朋友圈被折叠?会自动化不存在的(下)

AirPython  · 公众号  ·  · 2020-12-22 12:03

正文


点击上方“ AirPython ”,选择“ 加为星标

第一时间关注 Python 原创干货!


1. 前言

上篇文章,我们使用无障碍自动化服务实现了微信朋友圈内容防折叠的效果

自动化篇 | 朋友圈被折叠?会自动化不存在的

但使用一段时间后,发现这种方式并不灵活!

主要体现在:

  • 不灵活,偶尔会误处理

  • 在手机系统及 UI 元素层面,适配性不强

本篇将介绍另外一种实现方式,即:开发一款简易的输入法, 性强且 方便快捷

PS: 如果想直接下载体验,可以通过文末方式获取

2. 步骤

2-1 创建键盘服务

首先,使用 Android Studio 创建一个项目( 这里以 JAVA 为例,Kotlin 类似

然后,自定义一个系统键盘输入服务类

  • 继承于 InputMethodService

  • 实现 KeyboardView.OnKeyboardActionListener 接口,并重写方法

/***
 * 自定义系统键盘输入服务
 */


package com.xingag.inputx;

import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.KeyboardView;

/***
 * 自定义系统键盘输入服务
 */


public class CustomService extends InputMethodService implements KeyboardView.OnKeyboardActionListener
{
    @Override
    public void onPress(int primaryCode)
    
{

    }

    @Override
    public void onRelease(int primaryCode)
    
{

    }

    @Override
    public void onKey(int primaryCode, int [] keyCodes)
    
{

    }

    @Override
    public void onText(CharSequence text)
    
{

    }

    @Override
    public void swipeLeft()
    
{

    }

    @Override
    public void swipeRight()
    
{

    }

    @Override
    public void swipeDown()
    
{

    }

    @Override
    public void swipeUp()
    
{

    }
}

2-2 Manifest 配置键盘服务

在 AndroidManifest.xml 文件中,配置键盘服务,并 在 service 的 meta-data 中引用元数据

<service
    android:name=".CustomService"
    android:permission="android.permission.BIND_INPUT_METHOD">

    <intent-filter>
        <action android:name="android.view.InputMethod" />
    intent-filter>
    <meta-data
        android:name="android.view.im"
        android:resource="@xml/method" />

service>

元数据定义在 res/xml 目录下,内容如下:


<input-method xmlns:android="http://schemas.android.com/apk/res/android">
input-method>

需要注意的是, 元数据文件可以通过 subtype 标签指定语言类型、键盘模型;为了方便,这里使用默认配置即可

2-3 键盘布局文件

在 res/layout 创建一个键盘布局文件

指定键区的背景色、按键文字大小、按键点击前后颜色背景等属性



<android.inputmethodservice.KeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:keyBackground="@drawable/selector_key_background"
    android:keyPreviewOffset="0dp"
    android:keyTextColor="#000000"
    android:background="#b0b0b0"
    android:keyTextSize="20sp"
    android:shadowColor="@android:color/transparent"
    android:shadowRadius="0" />

接着,在 res/xml 中创建键盘按键展示的内容、布局、按键 Code 值

  • keyWidth:宽度;keyHeight:高度

  • horizontalGap/ verticalGap: 按键水平方向/垂直方向的间距

  • codes: 按键的 Code 值,方便事件处理

  • keyLabel: 按键上的文字展示内容

  • keyIcon: 按键的图标展示

  • isRepeatable: 代表按键是可重复的,如果为 True,则长按可以重复触发按键事件,默认值为 False

按键区定义了 4 个常见按键,分别对应: 防折叠输入、回退、长按清空、切换输入法

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:horizontalGap="1px"
    android:keyWidth="25%p"
    android:keyHeight="60dp"
    android:verticalGap="1px">

    <Row>
        <Key
            android:codes="1"
            android:keyLabel="开始输入" />


        <Key
            android:codes="2"
            android:keyIcon="@mipmap/ic_back" />

        
        <Key
            android:codes="4"
            android:isRepeatable="true"
            android:keyEdgeFlags="right"
            android:keyIcon="@mipmap/ic_clear"
            android:popupCharacters="xag" />

        <Key
            android:codes="3"
            android:keyLabel="切换输入法" />

    Row>
    <Row>
        <Key
            android:codes="0"
            android:keyWidth="100%p"
            android:keyHeight="60dp"
            android:keyEdgeFlags="right"
            android:keyLabel="防折叠输入法(公众号:AirPython)" />

    Row>
Keyboard>

2-4 创建键盘视图并设置监听

在键盘服务类中 onCreateInputView() 方法内,创建键盘视图并设置监听

@Override
public View onCreateInputView()
{
    @SuppressLint("InflateParams") KeyboardView keyboard_view = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard_view, null);
    Keyboard keyboard = new Keyboard(this, R.xml.keyboard);
    keyboard_view.setKeyboard(keyboard);
    keyboard_view.setOnKeyboardActionListener(this);

    //设置按键没有时,点击放大镜显示的效果
    setCandidatesViewShown(false);
    keyboard_view.setPreviewEnabled(false);
    return keyboard_view;
}

默认创建的键盘,点击按键时的放大镜效果会影响美观,建议通过 setCandidatesViewShown(false) 关闭它

2-5 处理按键点击事

重写 onKey(int key,int ints) 函数,其中 key 代表键盘 Code,通过它可以进行不同的事件处理

首先,我们拿到输入法的连接对象 InputConnection

使用它内置的 deleteSurroundingText() 方法即可以实现回退和长按清空的功能

InputConnection inputConnection = getCurrentInputConnection();

if (key == 1)
{
    //获取剪切板中的内容
    String clipContent = getClipContent();
    //模拟输入
    inputText(inputConnection, clipContent);
else if (key == 2)
{
    //回退
    inputConnection.deleteSurroundingText(10);
else if (key == 3)
{
    //切换输入法
    Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
else if (key == 4)
{
    //清空
    inputConnection.deleteSurroundingText(10);
}

对于朋友圈防折叠功能,我们依旧是分







请到「今天看啥」查看全文