2013-02-23 51 views
9

मैं कैसे एक पूर्णांक के लिए एक edittext इनपुट कन्वर्ट करने के लिए सोच रहा हूँ, मैं उपयोगकर्ता इनपुट एक संख्या है, यह तुलना में यह 8.संपादन टेक्स्ट को int में परिवर्तित करना? (Android)

MainActivity.java

@SuppressWarnings("unused") 
public void calcSpeed(View view) 
{  
    setContentView(R.layout.activity_speed);  

    final TextView mTextView = (TextView) findViewById(R.id.textView3); 
    mTextView.setText("You should be getting: " +netSpedCalcd); 
} 

activity_main.xml से विभाजित करके

<EditText 
    android:id="@+id/editText1" 
    android:inputType="number" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="62dp" 
    android:ems="10" > 
+0

इस जवाब http://stackoverflow.com/questions/4903515/how-do-i-return-an-int-from देखें:

बस संदर्भ के लिए, यहाँ मेरी एक्सएमएल है -edittext-android –

उत्तर

3

मैं बहुत नींद आ और अभी थक गया हूँ, लेकिन नहीं होगा यह काम ?:

EditText et = (EditText)findViewById(R.id.editText1); 
String sTextFromET = et.getText().toString(); 
int nIntFromET = new Integer(sTextFromET).intValue(); 

या

try 
{ 
    int nIntFromET = Integer.parseInt(sTextFromET); 
} 
catch (NumberFormatException e) 
{ 
    // handle the exception 
} 
+0

पहले के लिए, यह कहा गया है कि "मुझे हल नहीं किया जा सकता है" दूसरे के लिए, यह कहा गया है कि "Integer.parseInt को किसी प्रकार से हल नहीं किया जा सकता" – user2101454

+0

मैंने जवाब संपादित किया है (सहकर्मी समीक्षा का इंतजार कर रहा है), लेकिन दूसरे मामले में समस्या नकली "नई" है, बस इसे हटा दें। –

+0

ठीक है, sTextFromET के लिए, यह कहता है "sTextFromET को एक चर के लिए हल नहीं किया जा सकता", मैं क्या करूँ? (क्षमा करें, मैं सामान्य रूप से जावा और प्रोग्रामिंग में नया हूं।) – user2101454

8

उपयोग Integer.parseInt, और सुनिश्चित करें कि आप NumberFormatException कि यह फेंकता है तो इनपुट एक पूर्णांक नहीं है पकड़ने बनाते हैं।

7

आपको उपयोग करना होगा।

String value= et.getText().toString(); 
int finalValue=Integer.parseInt(value); 

यदि आपने केवल संख्या दर्ज करने की अनुमति दी है तो EditText प्रॉपर्टी सेट करें।

android:inputType="number" 

यदि यह सहायक है तो अन्यथा स्वीकार करें कि आपकी टिप्पणी दें।

+0

यह कहा गया "और हल नहीं किया जा सकता" – user2101454

+0

आपके कोड का पेस्ट कर सकते हैं? – Harshid

+0

एडिटटेक्स्ट एट = (एडिटटेक्स्ट) findViewById (R.id.ettext); इस तरह। – Harshid

1

इस

EditText x = (EditText) findViewById(R.id.editText1); 
int n = Integer.parseInt(x.toString()); 
+0

[काम करने की गारंटी नहीं है] (http://developer.android.com/reference/java/lang/Object.html#toString())। लिंक एक संकेत है। –

0

पूर्णांक editText कन्वर्ट करने के लिए नीचे दिए गए पंक्ति का प्रयास करें की कोशिश करो।

int intVal = Integer.parseInt(mEtValue.getText().toString()); 
0

मुझे एक ही समस्या थी। मुझे यकीन है कि अगर आप इसे हालांकि काम मिल गया है, लेकिन मैं क्या करना पड़ा था नहीं कर रहा हूँ:

EditText cypherInput; 
cypherInput = (EditText)findViewById(R.id.input_cipherValue); 
int cypher = Integer.parseInt(cypherInput.getText().toString()); 

कोड की तीसरी लाइन की वजह से एप्लिकेशन .toString से पहले .getText() का उपयोग किए बिना दुर्घटना() ।

<EditText 
    android:id="@+id/input_cipherValue" 
    android:inputType="number" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
0
int total_Parson = Integer.parseInt(etRegularTickets.getText().toString()); 
int ticket_price=Integer.parseInt(TicketData.get(0).getTicket_price_regular()); 
total_ticket_amount = ticket_price * total_Parson; 
etRegularPrice.setText(""+total_ticket_amount);