मेरे पास निम्न कोड है जिसका उपयोग मैं यह सुनिश्चित करने के लिए करना चाहता हूं कि मेरा संपादन खाली न हो। तो अगर पहले तैयार 0 (शून्य) निकाल दिया जाता है यह 0 पर वापस लौटने चाहिए फोकस बदल जाता है, यहाँ एप्लिकेशन अब तक है जब:संपादन को खाली होने से रोकें
package your.test.two;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class TesttwoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText edtxt = (EditText)findViewById(R.id.editText1);
// if I don't add the following the app crashes (obviously):
edtxt.setText("0");
edtxt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
update();
}
});
}
public void update() {
EditText edittxt = (EditText)findViewById(R.id.editText1);
Integer i = Integer.parseInt(edittxt.getText().toString());
// If i is an empty value, app crashes so if I erase the zero
//on the phone and change focus, the app crashes
}
}
मैं अद्यतन में निम्नलिखित की कोशिश की है() विधि:
String str = edittxt.getText().toString();
if (str == "") {
edittxt.setText("0");
}
लेकिन यह काम नहीं करता है। मैं edittext को कभी भी emty नहीं होने की अनुमति कैसे दे सकता हूं, खाली होने पर शून्य पर वापस लौटा सकता हूं लेकिन जब कोई मान मौजूद नहीं होता है। मैंने पहले से ही यह सुनिश्चित कर लिया है कि संपादन केवल संख्यात्मक मानों को ही अनुमति दे सकता है।
आपको बहुत बहुत धन्यवाद! यह काम किया। – Anomaly
यदि आप इस मुद्दे पर ठोकर खाते हैं, तो आपको 'बराबर() 'और' == 'के बीच अंतरों के बारे में पढ़ना चाहिए। @ क्रिस्टियन: शायद एक स्पष्टीकरण का थोड़ा और अच्छा होगा, क्योंकि इस मुद्दे से पता चलता है कि मूल बातें ओपी के लिए अस्पष्ट हैं :) – WarrenFaith