2017-06-12 16 views
-2

私はAndroid開発の初心者です。文字と整数で構成されるテキストビューを取得する必要があるアプリケーションに取り組んでいます。私はそのテキストビューから自分のEdittext内の整数だけを取得したい。私はEDITTEXTAndroidのedittextのテキストビューから値を取得

   <TextView 
       android:id="@+id/txt" 
       android:layout_width="118dp" 
       android:layout_height="38dp" 
       android:layout_marginLeft="38dp" 
       android:text="Sun Glasses \n Rs.729" 
       android:textColor="@color/colorAccent" 
       android:textSize="15sp" /> 

EDITTEXTのために取得したい

テキストはここにある:

 <EditText 
     android:id="@+id/editTextAmount" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAlignment="center" /> 

それを達成するための私の試み:私はすべてを試してみましたが、また、多くの続く

private EditText editTextAmount; 
private String paymentAmount; 

private String paymentAmount; 

editTextAmount = (EditText) findViewById(R.id.editTextAmount); 
amount=(TextView)findViewById(R.id.txt); 

editTextAmount.setText(amount.getText().toString()); 
paymentAmount = editTextAmount.getText().toString(); 

私の望む結果を得ることができません。 私は皆さんから助けを得てくれると嬉しく思います。

更新: 実際に私はPayPal Payment Gatewayを統合しています。画像をクリックすると、支払いページが表示され、価格はedittextで取得されます。

は、このコードの表情を持っている:

public class Activity_PayPal extends AppCompatActivity implements View.OnClickListener { 

//The views 
private Button buttonPay; 
private EditText editTextAmount; 
private TextView amount; 

//Payment Amount 
private String paymentAmount; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_pay); 

    buttonPay = (Button) findViewById(R.id.buttonPay); 
    editTextAmount = (EditText) findViewById(R.id.editTextAmount); 

    amount=(TextView)findViewById(R.id.txt); 

    buttonPay.setOnClickListener(this); 

    Intent intent = new Intent(this, PayPalService.class); 

    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config); 

    startService(intent); 
} 

@Override 
public void onClick(View v) { 
    getPayment(); 
} 

//Paypal intent request code to track onActivityResult method 
public static final int PAYPAL_REQUEST_CODE = 123; 


//Paypal Configuration Object 
private static PayPalConfiguration config = new PayPalConfiguration() 
     // Start with mock environment. When ready, switch to sandbox (ENVIRONMENT_SANDBOX) 
     // or live (ENVIRONMENT_PRODUCTION) 
     .environment(PayPalConfiguration.ENVIRONMENT_SANDBOX) 
     .clientId(PayPalConfig.PAYPAL_CLIENT_ID); 


@Override 
public void onDestroy() { 
    stopService(new Intent(this, PayPalService.class)); 
    super.onDestroy(); 
} 

private void getPayment() { 

    editTextAmount.setText(amount.getText().toString()); 
    //Getting the amount from editText 
    paymentAmount = editTextAmount.getText().toString(); 

    //Creating a paypalpayment 
    PayPalPayment payment = new PayPalPayment(new BigDecimal(String.valueOf(paymentAmount)), "USD", "Charges For Glasses", 
      PayPalPayment.PAYMENT_INTENT_SALE); 

    //Creating Paypal Payment activity intent 
    Intent intent = new Intent(this, PaymentActivity.class); 

    //putting the paypal configuration to the intent 
    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config); 

    //Puting paypal payment to the intent 
    intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment); 

    //Starting the intent activity for result 
    //the request code will be used on the method onActivityResult 
    startActivityForResult(intent, PAYPAL_REQUEST_CODE); 
} 


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    //If the result is from paypal 
    if (requestCode == PAYPAL_REQUEST_CODE) { 

     //If the result is OK i.e. user has not canceled the payment 
     if (resultCode == Activity.RESULT_OK) { 
      //Getting the payment confirmation 
      PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION); 

      //if confirmation is not null 
      if (confirm != null) { 
       try { 
        //Getting the payment details 
        String paymentDetails = confirm.toJSONObject().toString(4); 
        Log.i("paymentExample", paymentDetails); 

        //Starting a new activity for the payment details and also putting the payment details with intent 
        startActivity(new Intent(this, ConfirmationActivity.class) 
          .putExtra("PaymentDetails", paymentDetails) 
          .putExtra("PaymentAmount", paymentAmount)); 

       } catch (JSONException e) { 
        Log.e("paymentExample", "an extremely unlikely failure occurred: ", e); 
       } 
      } 
     } else if (resultCode == Activity.RESULT_CANCELED) { 
      Log.i("paymentExample", "The user canceled."); 
     } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) { 
      Log.i("paymentExample", "An invalid Payment or PayPalConfiguration was submitted. Please see the docs."); 
     } 
    } 
    } 
    } 
+0

なぜ複数の 'paymentAmount' ?? –

+0

あなたが望むものを正確に説明してください。 –

+0

@AhmadAghazadeh彼は 'EditText'が' TextView'のテキストの '729'部分だけを取得したいと考えています。しかし、あなたは実際に何も試していないが、テキストビューの正確なテキストをコピーしていたので、OP、あなたは本当にすべてを試しませんでした。 – Shark

答えて

0

このコードを試してみてください

String amount = amount.getText().toString(); 
String getNum = ExtractNumberFromString(amount); 
paymentAmount.setText(getNum); 

    public static String ExtractNumberFromString(String cnvrtdDate){ 
     String [] dateArray = cnvrtdDate.split("|"); 

     StringBuilder sb = new StringBuilder(); 
      StringBuilder sb1 = new StringBuilder(); 
     for(int i=0;i<dateArray.length;i++) 
     { 
      boolean ItIs = isNumeric(dateArray[i]); 
      if(ItIs) 
      { 

       sb.append(dateArray[i]); 
      } 
      else 
      { 
       sb1.append(dateArray[i]); 
      } 
     } 

    System.out.println("Numbers="+sb); 
    System.out.println("Characters="+sb1); 
     return sb.toString(); 
    } 


     public static boolean isNumeric(String str) 
     { 
      try 
      { 
       int d = Integer.parseInt(str); 
      } 
      catch(Exception nfe) 
      { 
       return false; 
      } 
      return true; 
     } 
0

この道を試してみてください。

String regex = "[0-9]+"; 
    String data = amount.getText().toString(); 

    Pattern pattern = Pattern.compile(regex); 
    List<String> list = new ArrayList<String>(); 
    Matcher m = pattern.matcher(data); 
    String text=""; 
    while (m.find()) { 
     text+=","+m.group(); 
    } 
    editTextAmount.setText(amount.getText().toString()); 
+0

thanks.Please、私の更新を見てください –

0

あなたがeditから文字列を抽出することができますテキストを作成し、正規表現を使用する文字列から数値を抽出する。何かのように

Pattern p = Pattern.compile("-?\\d+"); 
Matcher m = p.matcher(editTextAmount.getText().toString()); 
while (m.find()) { 
    System.out.println(m.group()); 
} 

これは、編集テキストのすべての数字を印刷します。

" - ?"負の数も同様に取り込むことができます。あなたはそれが負であることを疑うので、それを取り除くことができます。

関連する問題