0

我有一个严重的问题,我有一个通过所有卡片数据的刷卡器应用程序抛出了一个 xml(xml 在我的代码中的结构方式是由 sdk 提供程序编写的)。我需要使用 2 个 EditText(1 个用于卡号,1 个用于 ExpDate)进行手动输入,然后将该数据传递给同一活动(或一个新活动)以在处理订单的 xml 中使用。这可以做到吗?如何才能做到这一点?以下是我的代码 - 编辑或更正将非常有帮助......提前谢谢你。

String tranXmlFmt =  
        "<?xml version=\"1.0\"?><TStream>" 
      + "<Transaction>" 
      + "<IpAddress>66.173.160.125</IpAddress>" 
      + "<MerchantID>002649</MerchantID> " 
      + "<OperatorID>50</OperatorID>" 
      + "<TranType>Credit</TranType>" 
      + "<CardType>VISA</CardType> " 
      + "<TranCode>Sale</TranCode> " 
      + "<OperatorID>55</OperatorID> "
      + "<InvoiceNo>100001</InvoiceNo>" 
      + " <RefNo>100001</RefNo> " 
      + "<PartialAuth>Allow</PartialAuth>"
      + "<Account>" 
      + "<AcctNo>4104891389583005</AcctNo>"  
      + "</Account> " 
      + "<ExpDate>0214</ExpDate>"
      + "<Amount><Purchase>13.00</Purchase></Amount>" 
      + "<UserTraceData>GBTE130116</UserTraceData>"
      + "</Transaction> " 
      + "</TStream>";
String initXml = "" 
    + "<?xml version=\"1.0\"?>" 
    + "<TStream><Admin>" 
    + "    <MerchantID>002649</MerchantID>"
           + "    <TerminalID>002</TerminalID><TranCode>SecureDeviceInit</TranCode>"
           + "    <PadType>None</PadType>" + "    <TranType>Setup</TranType>"
           + "    <SecureDevice>UniMagSecureMagDsi</SecureDevice>" 
           + "    <ComPort>1</ComPort>" 
           + "  </Admin>"
           + "</TStream>";
    '

帮助

Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
    //Listening to button event
    btnNextScreen.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) { 
            //Starting a new Intent



        }
    });
      Button btnInit = (Button) findViewById(R.id.btnInit);
    btnInit.setOnClickListener(new View.OnClickListener() {

        /**
         * initialize epay client 
         */
        @Override
        public void onClick(View v) {
            libReady = true;
            ca = new ClientActivity(getApplication(), readerType);
            Log.d("MAINACTIVITY", "reader type: " + readerType.toString());
            initReturn = ca.initialize(initXml);
            Log.d("MAINACTIVITY", "init return: " + initXml);
            libReady = true;
            EditText et = (EditText) findViewById(R.id.editXml);
            et.setText(initReturn);
            Log.d("MAINACTIVITY", "Library Loaded");

        }
    });

    Button btnClose = (Button) findViewById(R.id.Close_button);
    btnClose.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            LineDisplay ld = new LineDisplay();
            ld.open(DeviceCommon.DEVICE_MODE_COMMON, null);

            ld.clear();             
            ld.setBacklight(LineDisplay.LCD_BACKLIGHT_OFF);

            ld.close();         
            finish();
        }
    });

我只需要添加 2 Edittext 并更改要在同一活动中使用的 AcctNo 和 ExpDate STRING。请任何帮助将是巨大的。

4

1 回答 1

1

You can get data from edittext and pass it to your secondactivtiy using intents.

     String s1,s2
     EditText ed1= (EditText)findViewById(R.id.editext); 
     EditText ed2= (EditText)findViewById(R.id.editext2);

         on Button click

     s1 = ed1.getText().toString(); 
     s2 = ed2.getText().toString();
     ed1.setText("");  
     ed2.setText("");  

In same activity. Can keep other tags as above.

     String xml ="<AcctNo>"+s1+"</AcctNo>"  
          + "<ExpDate>"+s2+"</ExpDate>"; 

             OR

    Intent i= new Intent("com.example.secondactivity");
    i.putExtra("key1",s1);   
    i.putExtra("key2",s2);  
    startActivity(i);

In Second Activity you can retrieve the values.

    String value1,value2;   
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
    value1 = extras.getString("key1"); 
    value2 = extras.getString("key2"); 
    }

 Can keep other tags as above.

    String xml ="<AcctNo>"+value1+"</AcctNo>"  
          + "<ExpDate>"+value2+"</ExpDate>";

EDIT :

acitivity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="33dp"
    android:ems="10" >
</EditText>
<EditText
    android:id="@+id/editText2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:ems="10" />
<TextView
   android:id="@+id/textView1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:layout_marginTop="36dp"
   android:text="TextView" />
 <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:text="TextView" />
  <TextView
      android:id="@+id/textView3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:layout_marginTop="20dp"
      android:text="TextView" />
    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:text="Button" />
    </LinearLayout>

MainActivity

   public class MainActivity extends Activity {

EditText ed1,ed2;
TextView tv1,tv2,tv3;
Button b;
String s1;
String s2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ed1= (EditText) findViewById(R.id.editText1);
    ed2= (EditText) findViewById(R.id.editText2);
    tv1 = (TextView) findViewById(R.id.textView1);
    tv2 = (TextView) findViewById(R.id.textView2);
    tv3 = (TextView) findViewById(R.id.textView3);
    b= (Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            s1= ed1.getText().toString();
            s2= ed2.getText().toString();
            System.out.println("................"+s1);
            System.out.println("................"+s2);
            tv1.setText(s1);
            tv2.setText(s2);
            String xml ="<AcctNo>"+s1+"</AcctNo>"  
                      + "<ExpDate>"+s2+"</ExpDate>";
            System.out.println("................"+xml);
            tv3.setText(xml);
            ed1.setText("");
            ed2.setText("");
        }

    });

}

}

As you can see it works. See the snap shot. I have 3 text views, 2 editext's and a button. After user types in editext's i click the button and i set the values to textview. As you can see the values reflects in string xml which is the result set in textview3.

enter image description here

于 2013-03-27T14:21:19.343 回答