我只是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.");
}
}
}
}
爲什麼多個'paymentAmount'? –
請準確解釋你想要的。 –
@AhmadAghazadeh他希望他的「EditText」只能得到'TextView'文本的'729'部分。但是,OP,你並沒有真正嘗試所有的東西,因爲你實際上什麼都沒做,只是複製textview的確切文本。 – Shark