2017-06-12 75 views
-2

我只是Android開發人員的初學者。我正在研究一個應用程序,我必須檢索由字符和整數組成的文本視圖。我想從textview中檢索我的Edittext中只有整數。Android中的edittext中的文本視圖獲取價值

文字,我想檢索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(); 

我都試過了,也跟着多教程,但沒有得到我想要的結果。 希望我會得到你們的幫助,我會很感激你們的幫助。

更新: 其實我正在整合貝寶支付網關,當我們點擊圖片時,付款頁面將出現,價格應該在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,你並沒有真正嘗試所有的東西,因爲你實際上什麼都沒做,只是複製textview的確切文本。 – 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

您可以提取編輯字符串文本,然後使用正則表達式從字符串中提取數字。類似於

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

這將打印編輯文本中的所有數字。

「 - ?」也允許它捕獲負數。你可以刪除,因爲我懷疑數量會是負面的。

相關問題