2017-09-26 155 views
0

您好,这是我的第一个问题 这里是情况:我正在一个服装软键盘上用特定的按钮来扫描条形码,它打开条形码扫描仪后,它扫描时,它会返回到前一个屏幕(比如消息传递应用程序),并显示剪辑或共享首选项的结果。除了EditText的结果来自旧扫描而非新扫描,因为当第一个活动获得结果时,此时扫描没有完成(在启动条形码扫描仪活动之前),所有的罚款和花花公子,除了试图让handler.postDelayed的关键功能用布尔测试,以等待扫描完成,但在启动扫描的结果我得到不响应错误 的关键功能在此之前冻结的活动是:自定义键盘扫描条形码并在EditText中插入结果

case 2017: 
      //Toast.makeText(FloatingViewService.this, "NormalScan.", Toast.LENGTH_LONG).show(); 
      test=true; 
      InputConnection ic = getCurrentInputConnection(); 
      ic.deleteSurroundingText(100, 100); //pour vider le champs de saisair 
      Intent dialogIntent = new Intent(SimpleIME.this, NormalScan.class); 
      dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(dialogIntent); 
      ic.commitText(String.valueOf(pref.getString("code", null)),1); 

NormalScan.class扫描条形码是:

public class NormalScan extends AppCompatActivity { 

    private ClipboardManager myClipboard; 
    private ClipData myClip; 

    @Override 

    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 


     IntentIntegrator integrator = new IntentIntegrator(this); 
     // integrator.setOrientationLocked(false); 
     integrator.setPrompt("Scan a barcode or QRcode"); 
     integrator.initiateScan(); 


     // integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES); 
//  Use this for more customization 
//integrator.setOrientationLocked(true); 
//  IntentIntegrator integrator = new IntentIntegrator(this); 

//  integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES); 

//  integrator.setPrompt("Scan a barcode"); 

//  integrator.setCameraId(0); // Use a specific camera of the device 

//  integrator.setBeepEnabled(false); 

//  integrator.setBarcodeImageEnabled(true); 

//  integrator.initiateScan(); 

    } 

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

     IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); 
     Iconify 
       .with(new FontAwesomeModule()) 
       .with(new EntypoModule()) 
       .with(new TypiconsModule()) 
       .with(new MaterialModule()) 
       .with(new MaterialCommunityModule()) 
       .with(new MeteoconsModule()) 
       .with(new WeathericonsModule()) 
       .with(new SimpleLineIconsModule()) 
       .with(new IoniconsModule());//// toast library 

     SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_WORLD_READABLE); 
     SharedPreferences.Editor editor = prefs.edit(); 

     if (result != null) { 
      if (result.getContents() == null) { 
       // Toast.makeText(this, "Opération annulée", Toast.LENGTH_LONG).show(); 
       //PrettyToast.showError(getApplicationContext(), "Opération annulée"); 
       new PrettyToast.Builder(getApplicationContext()) 
         .withMessage(" Opération annulée ✘ ") // ☑ ☒ ☓ ✓ ✓ ✕ ✖ ✗ ✘ 
         .withDuration(Toast.LENGTH_SHORT) 
         .withGravity(new PrettyToast.Gravity(Gravity.BOTTOM, 15, 0)) 
         .withTextSize(24) 
         .withBackgroundResource(R.drawable.background_toast_red) 
         .withTextColor(R.color.white) 
         .build() 
         .show(); 
       //myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE); 
       //String a = new String(); 
       //a=result.getContents(); 
       // myClip = ClipData.newPlainText("text","Scan Echouer"); 
       // myClipboard.setPrimaryClip(myClip); 
       final Handler handler = new Handler(); 
       handler.postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         //Do something after 100ms 

         handler.postDelayed(this, 2000); 
        } 
       }, 1500); 
       finish(); 
       setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
       //editor.putString("message", "annulée"); 
       //editor.putString("code", a); 
       //editor.commit(); 
      } else { 
       myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
       String a = new String(); 
       a = result.getContents(); 
       myClip = ClipData.newPlainText("text", a); 
       myClipboard.setPrimaryClip(myClip); 
       //PrettyToast.showSuccess(getApplicationContext(), "Le codebar sauvegarder dans la presse papier","",""); 
       new PrettyToast.Builder(getApplicationContext()) 
         .withMessage(" Scanned ✓ ") //☑ ✓ 
         .withDuration(Toast.LENGTH_SHORT) 
         .withGravity(new PrettyToast.Gravity(Gravity.BOTTOM, 15, 0)) 
         .withTextSize(24) 
         .withBackgroundResource(R.drawable.background_toast_green) 
         .withTextColor(R.color.white) 
         .build() 
         .show(); 
       //Toast.makeText(getApplicationContext(), "Le codebar sauvegarder dans la presse papier",Toast.LENGTH_SHORT).show(); 


       finish(); 
       editor.putString("message", "codebarre"); 
       editor.putString("code", a); 
       editor.putBoolean("testscan", true); 
       editor.commit(); 
       // show keybord 

        // InputMethodManager imm = (InputMethodManager) getSystemService(SimpleIME.INPUT_METHOD_SERVICE); 
        // imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0); 


      } 
     } 
     else { 
      // This is important, otherwise the result will not be passed to the fragment 
      super.onActivityResult(requestCode, resultCode, data); 

      finish(); 
     } 

    } 
} 

回答

1

启动活动的Wi startActivityForResult。当调用onActivityResult时,保存扫描的条形码。一个inputConnection成立,在下一次插入barcode-这应该是对的EditText

-1

How do I pass data between Activities in Android application?

Intent intent = new Intent(getBaseContext(), SignoutActivity.class); 
intent.putExtra("EXTRA_SESSION_ID", sessionId); 
startActivity(intent); 
Access that intent on next activity 

String s = getIntent().getStringExtra("EXTRA_SESSION_ID"); 

同样的原理,新的连接只需修改一下代码。

相关问题