我只是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