a57188731
@Ree
采纳率100%
2019-12-14 09:12 阅读 943

Android 手持PDA 如何让扫描到的数据,跟随光标,输入到任意EditText

最近在研究Android 手持PDA的扫描系统
从厂家获得到了demo的源代码,
按照厂家的源代码修改完之后。发现扫描到的内容只能输入到固定的文本框内
如果一个界面内有有两个文本框,就只能输入到第一个文本框内。

请问如何能根据光标的位置,输入到不同的文本框内

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:text="Scan Test"
        android:id="@+id/tv_title"
        android:textSize="40dp"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/tv_title"
        android:id="@+id/et1"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/et1"
        android:id="@+id/et2"/>
    <Button
        android:id="@+id/btnOpen"
        android:layout_below="@+id/et2"
        android:text="Scan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


</RelativeLayout>

JAVA文件

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.qs.service.ScanService;
import com.zebra.sdl.SDLguiActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private ScanBroadcastReceiver scanBroadcastReceiver;

    Button btnOpen, btnEdit;
    public static EditText et_code1;

    List<Map<String, String>> listData = new ArrayList<Map<String, String>>();

    String str2,str1,str;

    String TAG="数据";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        et_code1 = (EditText) findViewById(R.id.et1);

        et_code1.setText("");

        btnOpen = (Button) findViewById(R.id.btnOpen);
        btnOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                //斑马头软解码方式
                if(Build.MODEL.contains("mipi")){

                    SDLguiActivity.doDecode();

                }else{
                    //普通硬解码
                    ScanService.openScan();
                }


            }
        });

        //斑马头软解码方式
        if(Build.MODEL.contains("mipi")){

            //软解码为摄像头形式,需要加上权限
            if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, 1000);
            } else {
                //说明已经获取到摄像头权限了 想干嘛干嘛

                Intent newIntent = new Intent(MainActivity.this, SDLguiActivity.class);
                newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startService(newIntent);
            }



        }else{

            Intent newIntent = new Intent(MainActivity.this, ScanService.class);
            newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startService(newIntent);

        }

        scanBroadcastReceiver = new ScanBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.qs.scancode");
        this.registerReceiver(scanBroadcastReceiver, intentFilter);

    }

    @Override
    protected void onResume() {
        System.out.println("onResume" + "open");
        Log.v("onResume", "open");
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        this.unregisterReceiver(scanBroadcastReceiver);
        super.onDestroy();
    }

    class ScanBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            //text1为扫描信息
            String text1 = intent.getExtras().getString("code");
            String str = et_code1.getText().toString().trim()+"\n";
            et_code1.setText(str + text1);
            Editable text = et_code1.getText();
            Spannable spanText = text;
            Selection.setSelection(spanText, text.length());
        }
    }

}

求大神指点,本人是初学者,麻烦详细一点。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    Dear_xkk 渊源yuan缘愿 2019-12-20 14:11

    PDA扫描到信息后,先找到当前有焦点的控件
    View rootview = activity.getWindow().getDecorView();
    View focusView = rootview.findFocus();
    然后给他设置值
    if (focusView instanceof EditText) {
    EditText et = (EditText) focusView;
    et.setText("你要展示的值")
    }

    点赞 评论 复制链接分享

相关推荐