我可以将startActivityForResult与一项活动一起使用吗?

时间:2018-08-08 07:31:50

标签: java android

编辑:更新了我的代码,发布了更多代码供您查看。 编辑:为什么我被否决? EDIT2:破坏了我的代码,大声笑,我要撤消所有您的建议,然后尝试提供的xml选项

我想知道我是否可以在主要活动中使用- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return [UIScreen mainScreen].bounds.size.width; } 。 我正在使用以下代码打开startActivityForResult, 我看到的第二个scannerview是什么。

我怎么能得到结果,因为这并不是正式的另一项活动,而仅仅是一种方法。

activiy

XML

   Button sendButton;
 //EditText edt4;
 EditText edt2;

@SuppressLint("CutPasteId")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    edt4 = findViewById(R.id.editText4);
    ZXingScannerView mScannerView = findViewById(xmlScannerView););
 @Override
public void onResume() {
    super.onResume();
    mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
    mScannerView.startCamera();          // Start camera on resume
}

@Override
public void onPause() {
    super.onPause();
    mScannerView.stopCamera();           // Stop camera on pause
}

public void onClick(View v){
    ZXingScannerView mScannerView = new ZXingScannerView(this);
   mScannerView.setVisibility(View.VISIBLE);       
   mScannerView.setResultHandler(this);
    mScannerView.startCamera();


}




//EditText editText4;



EditText edt4;
@Override
public void handleResult(final Result result) {
    //handle result

    Log.v("handleResult", result.getText());
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Ordernummer of Locatie");
    builder.setMessage(result.getText());
    AlertDialog alertDialog = builder.create();
    alertDialog.show();

    //edt4.setText(result.getText());
    //edt4 = findViewById(editText4);

    //edt4.setText(String.valueOf(result.getText()));

    runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          updateScannerData(1,result.getText());
                      }


});
}

private void updateScannerData(int scanType, String scannedCode){

    //startActivity(new Intent(this,MainActivity.class));
    //this.finish();
    edt4.setText(scannedCode);
}



@Override
public void onBackPressed()
{
    startActivity(new Intent(this,MainActivity.class));
    this.finish();
}

2 个答案:

答案 0 :(得分:0)

好吧,我认为我们需要看一下您当前的代码和ValueError Traceback (most recent call last) <ipython-input-2-879de613d8dc> in <module>() 1 from azureml import Workspace ----> 2 ws = Workspace() 3 experiment = ws.experiments['04cff8814e1949fd957b40192cc7eac7.f-id.3be55208572740e08a9a4c7937398866'] 4 ds = experiment.get_intermediate_dataset( 5 node_id='4dc79617-ac81-45ba-a807-4e646ae1de29-403', /home/nbuser/anaconda3_23/lib/python3.4/site-packages/azureml/__init__.py in __init__(self, workspace_id, authorization_token, endpoint) 883 endpoint = https://studio.azureml.net 884 """ --> 885 workspace_id, authorization_token, endpoint, management_endpoint = _get_workspace_info(workspace_id, authorization_token, endpoint, None) 886 887 _not_none_or_empty('workspace_id', workspace_id) /home/nbuser/anaconda3_23/lib/python3.4/site-packages/azureml/__init__.py in _get_workspace_info(workspace_id, authorization_token, endpoint, management_endpoint) 849 850 if workspace_id is None: --> 851 raise ValueError('workspace_id not provided and not available via config') 852 if authorization_token is None: 853 raise ValueError('authorization_token not provided and not available via config') ValueError: workspace_id not provided and not available via config 。如果我们看一下ZXingScannerView的Github存储库,有一个示例与您的实现有所不同。

首先,您应该仅在您的ZXingScannerView的{​​{1}}中呼叫setContentView(mScannerView);。现在,当您单击时,整个视图将重新绘制,这是不需要的。每次单击按钮时,还需要设置Activity,这也可能会引起问题。

此代码摘自您正在使用的元素的Github存储库,因此请使您的onCreate()如下所示。

https://github.com/dm77/barcodescanner

resultHandler

还请注意,您将需要访问手机摄像头的权限。

答案 1 :(得分:0)

不,您不能在活动的布局中使用多个layout groups或使用多个片段。

如果您选择第一个选项,则应在您的XML布局中添加ZXingScannerView,如果您想使用它,可以简单地纠缠它的可见性

<me.dm7.barcodescanner.zxing.ZXingScannerView
android:width="match_parent"
height="match_parent"
android:id="someId"
android:visibility="gone"
/>

然后在您的代码中

         private ZXingScannerView mScannerView;

            @Override
            public void onCreate(Bundle state) {
                super.onCreate(state);
                setContentView(someLayout);    

                mScannerView = findViewById(SomeId);     
    mScannerView.setFormats(ZXingScannerView.ALL_FORMATS);// dont forget this
mScannerView.setResultHandler(this);
            mScannerView.startCamera()
            }
            public void onClick(View v){
              mScannerView.setVisibility(View.Visible);


            }

编辑 致电setFormats(见上文),然后启动相机,进入onCreate(),并将XML的可见性更改为INVISIBLE

编辑2

您的XML应该是这样的

     <FrameLayout 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:background="@mipmap/ic_launcher_foreground">


        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">

    <me.dm7.barcodescanner.zxing.ZXingScannerView

        android:id="@+id/xmlScannerView"
        android:visibility="gone"
        android:layout_height="match_parent"
        android:layout_width="match_parent" /> 
    <RelativeLayout

        android:id="@+id/someId"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <EditText
            android:id="@+id/editText4"
            android:layout_width="match_parent"
            android:layout_height="62dp"
            android:layout_marginTop="67dp"
            android:ems="10"
            android:hint="@string/scan_locatie"
            android:inputType="text"

            android:text=""
            tools:backgroundTint="@android:color/holo_red_light" />


        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/editText4"
            android:layout_centerHorizontal="true"
            android:background="@android:color/holo_red_light"
            android:onClick="onClick"
            android:text="@string/scan_qr"
            tools:text="scan qr code" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="61dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="197dp"
            android:ems="10"
            android:hint="@string/scan_order"

            android:inputType=""
            android:visibility="visible"
            tools:backgroundTint="@android:color/holo_red_light" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/editText2"
            android:layout_centerHorizontal="true"
            android:background="@android:color/holo_red_light"
            android:onClick="onClick"
            android:text="@string/scan_qr"
            tools:text="scan qr code" />

        <Button
            android:id="@+id/sendButton"
            android:layout_width="157dp"
            android:layout_height="32dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="72dp"
            android:background="@android:color/holo_red_light"
            android:text="@string/button"
            tools:text="Versturen.." />


        <Button
            android:id="@+id/button3"
            android:layout_width="40dp"
            android:layout_height="38dp"
            android:layout_alignBaseline="@+id/editText2"
            android:layout_alignParentEnd="true"
            android:background="@android:drawable/ic_delete" />

        <Button
            android:id="@+id/button4"
            android:layout_width="39dp"
            android:layout_height="37dp"
            android:layout_alignBaseline="@+id/editText4"
            android:layout_alignParentEnd="true"
            android:background="@android:drawable/ic_delete" />

    </RelativeLayout>
</FrameLayout>

然后在调用onClick

  public void onClick(View v){

              yourRelativeLayout.setVisibility(View.Invisible);
              mScannerView.setVisibility(View.Visible);


            }