简体   繁体   English

Android Java,在应用程序外部设置字段文本

[英]Android Java, Setting text of a field outside of the application

I'm currently writing an Android application that will allow a user to input the result of a scanned barcode into a field outside of the application. 我目前正在编写一个Android应用程序,该程序将允许用户将扫描条形码的结果输入到应用程序外部的字段中。

The app will simply be a Floating Button that when pressed, will bring up a camera screen that scans and reads the barcode being pointed at, and save the result as an intent. 该应用程序将只是一个浮动按钮,按下该按钮将显示一个摄像头屏幕,可扫描并读取所指向的条形码,并将结果保存为意图。

The user will primarily be working on Chrome when using this Floating Button. 使用此浮动按钮时,用户将主要在Chrome上工作。

Question: Is it possible to set the text of the current field in focus, outside of the application, as the result of the scanned barcode? 问题:是否可以将当前字段的文本设置为应用程序外部的焦点,作为扫描条形码的结果?

I'm using the following libraries: - com.google.zxing:core:3.3.3 - com.journeyapps:zxing-android-embedded:3.5.0@aar 我正在使用以下库:-com.google.zxing:core:3.3.3-com.journeyapps:zxing-android-embedded:3.5.0@aar

Current code: 当前代码:

public class MainActivity extends AppCompatActivity {

FloatingActionButton fabScan;

TextView textViewScanResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Activity activity = this;

    textViewScanResult = findViewById(R.id.textViewScan);

    fabScan = findViewById(R.id.floatingActionButton_scan);

    fabScan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            IntentIntegrator integrator = new IntentIntegrator(activity);
            integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
            integrator.setPrompt("Scan Barcode");
            integrator.setCameraId(0);
            integrator.setBeepEnabled(true);
            integrator.setBarcodeImageEnabled(false);
            integrator.setOrientationLocked(false);
            integrator.initiateScan();

        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

    if (result != null) {

        if (result.getContents() == null) {
            Toast.makeText(getApplicationContext(), "nothing", Toast.LENGTH_SHORT).show();
        } else {
            textViewScanResult.setText(result.getContents());
        }

    } else {

        super.onActivityResult(requestCode, resultCode, data);

    }
}

Currently, i'm only setting a textView as the scanned result. 目前,我仅将textView设置为扫描结果。

try setting browser intent with the result content. 尝试使用结果内容设置浏览器意图。

@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { @Override protected void onActivityResult(int requestCode,int resultCode,@Nullable Intent data){

IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

if (result != null) {

    if (result.getContents() == null) {
        Toast.makeText(getApplicationContext(), "nothing", Toast.LENGTH_SHORT).show();
    } else {
        textViewScanResult.setText();

         //here
         Intent intent = new Intent(Intent.ACTION_VIEW, 
         Uri.parse("http://www.stackoverflow.com")); //change url to google
         intent.putExtra(result.getContents().toString());
         startActivity(intent);
    }

} else {

    super.onActivityResult(requestCode, resultCode, data);

}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM