本文介绍了EDITTEXT:错误解决再启用机器人发送按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有五个的EditText字段被检查领域,并限制时输入错误的一些数据,如下图所示code,但现在如果我的EditText输入错误的数据,然后将这些信息输入数据库,所以我想禁用钮,如果有任何的EditText错误。该错误可能会在1到5的EditText它有禁用发送按钮。它必须只启用了发送按钮时,没有任何错误的领域的EditText

 公共类MaINActivity延伸活动{

    字符串结果= NULL;
    InputStream的是= NULL;
    字符串VALID_EMAIL = NULL;
    字符串valid_name = NULL;
    字符串valid_last = NULL;
    字符串valid_age = NULL;
    字符串V5,V1,V2,V3,V4,V6,
    字符串valid_confirm = NULL;
@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_subscribe);

    最后的EditText EDITTEXT =(EditText上)findViewById(R.id.firstname);

    最后的EditText editText1 =(EditText上)findViewById(R.id.lastname);

    最后的EditText editText2 =(EditText上)findViewById(R.id.age1);

    最后的EditText editText3 =(EditText上)findViewById(R.id.email);

    最后的EditText editText4 =(EditText上)findViewById(R.id.confirm);

    editText2.addTextChangedListener(新TextWatcher(){

        @覆盖
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
                之后INT){
        }

        @覆盖
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,
                诠释计数){
        }

        @覆盖
        公共无效afterTextChanged(编辑S){
            Is_Valid_Age(editText2);
        }

        私人无效Is_Valid_Age(EditText上editText2){

            如果(editText2.length()== 0 || editText2.equals()|| editText2 == NULL ||.equals(editText2.getText()的toString())){
                valid_age = NULL;
            }其他 {
                。valid_age = editText2.getText()的toString();
            }
        }

    });
    editText1.addTextChangedListener(新TextWatcher(){

        @覆盖
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
                之后INT){


        }

        @覆盖
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,
                诠释计数){


        }

        @覆盖
        公共无效afterTextChanged(编辑S){
            Is_Valid_Last_Name(editText1);

        }

        私人无效Is_Valid_Last_Name(EditText上editText1)抛出NumberFormatException的{

            如果(editText1.getText()的toString()长度()< = 0){
                editText1.setError(接受字母而已。);
                valid_last = NULL;
            }否则,如果(!editText1.getText()的toString()。匹配([A-ZA-Z] +)){
                editText1.setError(接受字母而已。);
                valid_last = NULL;
            }否则,如果(editText1.length()== 0 || editText1.equals()|| editText1 == NULL ||.equals(editText1.getText()的toString())){
                valid_last = NULL;
            }其他 {
                。valid_last = editText1.getText()的toString();
            }

        }

    });

    editText.addTextChangedListener(新TextWatcher(){

        @覆盖
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
                之后INT){


        }

        @覆盖
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,
                诠释计数){


        }

        @覆盖
        公共无效afterTextChanged(编辑S){
            Is_Valid_First_Name(EDITTEXT);

        }

        私人无效Is_Valid_First_Name(EditText上EDITTEXT)抛出NumberFormatException的{

            如果(editText.getText()的toString()长度()< = 0){
                editText.setError(接受字母而已。);
                valid_name = NULL;
            }否则,如果(!editText.getText()的toString()。匹配([A-ZA-Z] +)){
                editText.setError(接受字母而已。);
                valid_name = NULL;
            }否则,如果(editText.length()== 0 || editText.equals()|| EDITTEXT == NULL ||.equals(editText.getText()的toString())){
                valid_name = NULL;
            }其他 {
                。valid_name = editText.getText()的toString();
            }

        }
    });

    editText3.addTextChangedListener(新TextWatcher(){
        @覆盖
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,
                诠释计数){

        }

        @覆盖
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
                之后INT){

        }

        @覆盖
        公共无效afterTextChanged(编辑S){

            Is_Valid_Email_Address(editText3);
        }

        公共无效Is_Valid_Email_Address(EditText上editText3){
            如果(editText3.getText()的toString()== NULL){
                editText3.setError(电子邮件地址无效);
                VALID_EMAIL = NULL;
            }否则,如果(isEmailValid(editText3.getText()的toString())== FALSE){
                editText3.setError(电子邮件地址无效);
                VALID_EMAIL = NULL;
            }否则,如果(editText3.length()== 0 || editText3.equals()|| editText3 == NULL ||.equals(editText3.getText()的toString())){
                VALID_EMAIL = NULL;
            }其他 {
                。VALID_EMAIL = editText3.getText()的toString();
            }
        }
    });

    editText4.addTextChangedListener(新TextWatcher(){

        @覆盖
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,
                诠释计数){


        }

        @覆盖
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
                之后INT){


        }

        @覆盖
        公共无效afterTextChanged(编辑S){

            Is_Valid_Confirm_Address(editText4);
        }


        公共无效Is_Valid_Confirm_Address(EditText上editText4){
            如果(editText4.getText()的toString()== NULL){
                editText4.setError(电子邮件地址无效);
                valid_confirm = NULL;
            }否则,如果(isEmailValid(editText4.getText()的toString())== FALSE){
                editText4.setError(电子邮件地址无效);
                valid_confirm = NULL;
            }否则,如果(editText4.getText()的toString()。等于(editText3.getText()的toString())){

                。valid_confirm = editText4.getText()的toString();
            }否则,如果(editText4.length()== 0 || editText4.equals()|| editText4 == NULL ||.equals(editText4.getText()的toString())){
                valid_confirm = NULL;
            } 其他 {
                editText4.setError(确认电子邮件地址是不匹配);
                valid_confirm = NULL;
            }

        }
    });

    按钮发送=(按钮)findViewById(R.id.send);
    send.setOnClickListener(新View.OnClickListener(){

        @燮pressLint(NewApi)
        @覆盖
        公共无效的onClick(视图查看){

            V1 = editText.getText()的toString()。
            V2 = editText1.getText()的toString()。
            V3 = editText2.getText()的toString()。
            V4 = editText3.getText()的toString()。
            V5 = editText4.getText()的toString()。


            ArrayList的<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>();

            nameValuePairs.add(新BasicNameValuePair(名字,V1));
            nameValuePairs.add(新BasicNameValuePair(姓氏,V2));
            nameValuePairs.add(新BasicNameValuePair(时代,第三版));
            nameValuePairs.add(新BasicNameValuePair(EMAIL_ADDRESS,V4));
            nameValuePairs.add(新BasicNameValuePair(confirm_email_address,第5版));



            StrictMode.setThreadPolicy(政策);

            尝试
            {
                HttpClient的HttpClient的=新DefaultHttpClient();
                HttpPost httppost =新HttpPost(本地主机/安卓/ Insert.php);
                httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
                HTT presponse响应= httpclient.execute(httppost);
                HttpEntity实体= response.getEntity();
                是= entity.getContent();

                Log.e(log_tag,连接成功);
                Toast.makeText(getApplicationContext(),认购Successfull.Thank您订阅你现在已经被添加到我们的邮件列表,Toast.LENGTH_LONG).show();
            }
            赶上(例外五)
            {
                Log.e(log_tag,错误的HTTP连接+ e.toString());
                Toast.makeText(getApplicationContext(),连接失败,Toast.LENGTH_SHORT).show();
            }
            尝试
            {
                的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(就是,ISO-8859-1),8);
                StringBuilder的SB =新的StringBuilder();
                串线= NULL;
                而((行= reader.readLine())!= NULL)
                {
                        sb.append(行+\ N);
                        意图I =新的意图(getBaseContext(),SecondActivity.class);
                        startActivity(ⅰ);
                }
                is.close();

                结果= sb.toString();
            }
            赶上(例外五)
            {
               Log.e(log_tag,错误转换结果+ e.toString());
            }
            尝试
            {
                    JSONObject的json_data =新的JSONObject的(结果);
                    CharSequence的W =(CharSequence的)json_data.get(结果);

                    Toast.makeText(getApplicationContext(),瓦特,Toast.LENGTH_SHORT).show();
            }
            赶上(JSONException E)
            {
                    Log.e(log_tag,错误分析数据+ e.toString());
                    Toast.makeText(getApplicationContext(),JsonArray失败,Toast.LENGTH_SHORT).show();
            }
        }


    });

}

    私人布尔isEmailValid(字符串电子邮件){

        返回android.util.Patterns.EMAIL_ADDRESS.matcher(电子邮件).matches();
    }}
 

解决方案

试试这个办法,希望这将帮助你解决你的问题。

activity_subscribe.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    机器人:重力=中心
    机器人:填充=5DP>

    <滚动型
        机器人:layout_width =match_parent
        机器人:layout_height =0dp
        机器人:layout_weight =1>
        <的LinearLayout
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:方向=垂直>

            <的EditText
                机器人:ID =@ + ID / edtFirstName
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:提示=姓
                机器人:layout_marginTop =5DP/>

            <的EditText
                机器人:ID =@ + ID / edtLastName
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:提示=姓
                机器人:layout_marginTop =5DP/>
            <的EditText
                机器人:ID =@ + ID / edtAge
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:提示=时代
                机器人:layout_marginTop =5DP/>
            <的EditText
                机器人:ID =@ + ID / edtEmail
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:提示=电子邮件
                机器人:inputType =textEmailAddress
                机器人:layout_marginTop =5DP/>
            <的EditText
                机器人:ID =@ + ID / edtPassword
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:inputType =textPassword
                机器人:提示=密码
                机器人:layout_marginTop =5DP/>
            <的EditText
                机器人:ID =@ + ID / edtConfirmPassword
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:inputType =textPassword
                机器人:提示=确认密码
                机器人:layout_marginTop =5DP/>
            < / LinearLayout中>
        < /滚动型>
    <按钮
        机器人:ID =@ + ID / btnSubmit按钮
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=提交/>

< / LinearLayout中>
 

SubscribeActivty.java

 公共类SubscribeActivty延伸活动{

    InputStream的是= NULL;
    私人的EditText edtFirstName;
    私人的EditText edtLastName;
    私人的EditText edtAge;
    私人的EditText edtEmail;
    私人的EditText edtPassword;
    私人的EditText edtConfirmPassword;
    私人按钮btnSubmit按钮;


    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_subscribe);

        edtFirstName =(EditText上)findViewById(R.id.edtFirstName);
        edtLastName =(EditText上)findViewById(R.id.edtLastName);
        edtAge =(EditText上)findViewById(R.id.edtAge);
        edtEmail =(EditText上)findViewById(R.id.edtEmail);
        edtPassword =(EditText上)findViewById(R.id.edtPassword);
        edtConfirmPassword =(EditText上)findViewById(R.id.edtConfirmPassword);
        btnSubmit按钮=(按钮)findViewById(R.id.btnSubmit);

        btnSubmit.setOnClickListener(新View.OnClickListener(){

            @燮pressLint(NewApi)
            @覆盖
            公共无效的onClick(视图查看){

                布尔的isValid = TRUE;
                如果(edtFirstName.getText()的toString()长度()< = 0){
                    edtFirstName.setError(必需值);
                    的isValid = FALSE;
                }否则,如果(!edtFirstName.getText()的toString()。匹配([A-ZA-Z] +)){
                    edtFirstName.setError(接受字母而已。);
                    的isValid = FALSE;
                }

                如果(edtLastName.getText()的toString()长度()< = 0){
                    edtLastName.setError(必需值);
                    的isValid = FALSE;
                }否则,如果(!edtLastName.getText()的toString()。匹配([A-ZA-Z] +)){
                    edtLastName.setError(接受字母而已。);
                    的isValid = FALSE;
                }

                如果(edtEmail.getText()的toString()长度()< = 0){
                    edtEmail.setError(必需值);
                    的isValid = FALSE;
                }否则,如果(!isEmailValid(edtEmail.getText()的toString())){
                    edtLastName.setError(无效的电子邮件);
                    的isValid = FALSE;
                }

                如果(edtAge.getText()的toString()长度()< = 0){
                    edtAge.setError(必需值);
                    的isValid = FALSE;
                }否则,如果(!edtAge.getText()的toString()。匹配([0-9] +)){
                    edtAge.setError(接受数字而已。);
                    的isValid = FALSE;
                }

                如果(edtPassword.getText()的toString()长度()< = 0){
                    edtPassword.setError(必需值);
                    的isValid = FALSE;
                }

                如果(edtConfirmPassword.getText()的toString()长度()< = 0){
                    edtConfirmPassword.setError(必需值);
                    的isValid = FALSE;
                }否则,如果(!edtConfirmPassword.getText()的toString()。等于(edtPassword.getText()的toString())){
                    edtConfirmPassword.setError(密码不匹配。);
                    的isValid = FALSE;
                }


                如果(参考isValid){
                    ArrayList的<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>();

                    nameValuePairs.add(新BasicNameValuePair(名字,edtFirstName.getText()的toString()));
                    nameValuePairs.add(新BasicNameValuePair(姓氏,edtLastName.getText()的toString()));
                    nameValuePairs.add(新BasicNameValuePair(时代,edtAge.getText()的toString()));
                    nameValuePairs.add(新BasicNameValuePair(电子邮件,edtEmail.getText()的toString()));
                    nameValuePairs.add(新BasicNameValuePair(密码,edtPassword.getText()的toString()));

                    submitDataOnServer(namevaluepairs中);
                }
            }

        });

    }

    公共无效submitDataOnServer(最终的ArrayList<的NameValuePair> namevaluepairs中){
        新的AsyncTask<虚空,虚空,字符串>(){
            @覆盖
            保护字符串doInBackground(空... PARAMS){
                尝试 {
                    HttpClient的HttpClient的=新DefaultHttpClient();
                    HttpPost httppost =新HttpPost(本地主机/安卓/ Insert.php);
                    httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
                    HTT presponse响应= httpclient.execute(httppost);
                    HttpEntity实体= response.getEntity();
                    是= entity.getContent();

                    Log.e(log_tag,连接成功);
                    Toast.makeText(getApplicationContext(),认购Successfull.Thank您订阅你现在已经被添加到我们的邮件列表,Toast.LENGTH_LONG).show();
                }赶上(例外五){
                    Log.e(log_tag,错误的HTTP连接+ e.toString());
                    Toast.makeText(getApplicationContext(),连接失败,Toast.LENGTH_SHORT).show();
                }
                尝试 {

                    的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(就是,ISO-8859-1),8);
                    StringBuilder的SB =新的StringBuilder();
                    串线= NULL;
                    而((行= reader.readLine())!= NULL){
                        sb.append(行+\ N);
                    }
                    is.close();

                    返回sb.toString();
                }赶上(例外五){
                    Log.e(log_tag,错误转换结果+ e.toString());
                }
                返回null;
            }

            @覆盖
            保护无效onPostExecute(字符串结果){
                super.onPostExecute(结果);
                尝试 {
                    JSONObject的json_data =新的JSONObject的(结果);
                    CharSequence的W =(CharSequence的)json_data.get(结果);

                    Toast.makeText(getApplicationContext(),瓦特,Toast.LENGTH_SHORT).show();
                }赶上(JSONException E){
                    Log.e(log_tag,错误分析数据+ e.toString());
                    Toast.makeText(getApplicationContext(),JsonArray失败,Toast.LENGTH_SHORT).show();
                }
            }
        }。执行();
    }


    公共布尔isEmailValid(最后弦乐mailAddress){
        模式模式;
        匹配匹配;
        最后弦乐EMAIL_PATTERN =^ [_ A-ZA-Z0-9  -  \\ +] +(\\ [_ A-ZA-Z0-9  - ] +)* @+[A-ZA-z0-9- ] +(。\\ [A-ZA-Z0-9] +)*(。\\ [A-ZA-Z] {2})$;
        模式= Pattern.compile(EMAIL_PATTERN);
        匹配= pattern.matcher(mailAddress);
        返回matcher.matches();
    }
}
 

i have five edittext fields which is checking the fields and restricts some data when entered wrong as shown below code but now if i enter wrong data in edittext then it will enter the information into database so i want to disable the button if there are errors in any edittext . The error might be in 1 to 5 edittext it has to disable the send button. It has to only enable the send button when there are no errors in the edittext fields

public class MaINActivity extends Activity {

    String result = null;
    InputStream is = null;
    String valid_email = null;
    String valid_name = null;
    String valid_last = null;
    String valid_age = null;
    String v5,v1,v2,v3,v4,v6;
    String valid_confirm = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_subscribe);

    final EditText editText = (EditText)findViewById(R.id.firstname);

    final EditText editText1 = (EditText)findViewById(R.id.lastname);

    final EditText editText2 = (EditText)findViewById(R.id.age1);

    final EditText editText3 = (EditText)findViewById(R.id.email);

    final EditText editText4 = (EditText)findViewById(R.id.confirm);

    editText2.addTextChangedListener(new TextWatcher(){

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            Is_Valid_Age(editText2);
        }

        private void Is_Valid_Age(EditText editText2) {

            if(editText2.length() == 0 || editText2.equals("") || editText2 == null || "".equals(editText2.getText().toString())) {
                valid_age = null;
            }else {
                valid_age = editText2.getText().toString();
            }
        }

    });
    editText1.addTextChangedListener(new TextWatcher(){

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {


        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {


        }

        @Override
        public void afterTextChanged(Editable s) {
            Is_Valid_Last_Name(editText1);

        }

        private void Is_Valid_Last_Name(EditText editText1) throws NumberFormatException {

            if (editText1.getText().toString().length() <= 0) {
                editText1.setError("Accept Alphabets Only.");
                valid_last = null;
            } else if (!editText1.getText().toString().matches("[a-zA-Z ]+")) {
                editText1.setError("Accept Alphabets Only.");
                valid_last = null;
            } else if(editText1.length() == 0 || editText1.equals("") || editText1 == null || "".equals(editText1.getText().toString())) {
                valid_last = null;
            }else {
                valid_last = editText1.getText().toString();
            }

        }

    });

    editText.addTextChangedListener(new TextWatcher(){

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {


        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {


        }

        @Override
        public void afterTextChanged(Editable s) {
            Is_Valid_First_Name(editText);

        }

        private void Is_Valid_First_Name(EditText editText) throws NumberFormatException {

            if (editText.getText().toString().length() <= 0) {
                editText.setError("Accept Alphabets Only.");
                valid_name = null;
            } else if (!editText.getText().toString().matches("[a-zA-Z ]+")) {
                editText.setError("Accept Alphabets Only.");
                valid_name = null;
            } else if(editText.length() == 0 || editText.equals("") || editText == null || "".equals(editText.getText().toString())) {
                valid_name = null;
            }else {
                valid_name = editText.getText().toString();
            }

        }
    }); 

    editText3.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

            Is_Valid_Email_Address(editText3);
        }

        public void Is_Valid_Email_Address(EditText editText3) {
            if (editText3.getText().toString() == null) {
                editText3.setError("Invalid Email Address");
                valid_email = null;
            } else if (isEmailValid(editText3.getText().toString()) == false) {
                editText3.setError("Invalid Email Address");
                valid_email = null;
            } else if(editText3.length() == 0 || editText3.equals("") || editText3 == null || "".equals(editText3.getText().toString())) {
                valid_email = null;
            }else {
                valid_email = editText3.getText().toString();
            }
        }
    });

    editText4.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {


        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {


        }

        @Override
        public void afterTextChanged(Editable s) {

            Is_Valid_Confirm_Address(editText4);
        }


        public void Is_Valid_Confirm_Address(EditText editText4) {
            if (editText4.getText().toString() == null) {
                editText4.setError("Invalid Email Address");
                valid_confirm = null;
            } else if (isEmailValid(editText4.getText().toString()) == false) {
                editText4.setError("Invalid Email Address");
                valid_confirm = null;
            } else if (editText4.getText().toString().equals(editText3.getText().toString())) {

                valid_confirm = editText4.getText().toString();
            } else if(editText4.length() == 0 || editText4.equals("") || editText4 == null || "".equals(editText4.getText().toString())) {
                valid_confirm = null;
            } else {
                editText4.setError("Confirm Email is Not Matching");
                valid_confirm = null;
            } 

        }
    });

    Button send = (Button) findViewById(R.id.send);
    send.setOnClickListener(new View.OnClickListener(){

        @SuppressLint("NewApi")
        @Override
        public void onClick(View view) {

            v1 = editText.getText().toString();
            v2 = editText1.getText().toString();
            v3 = editText2.getText().toString();
            v4 = editText3.getText().toString();
            v5 = editText4.getText().toString();  


            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            nameValuePairs.add(new BasicNameValuePair("firstname",v1));
            nameValuePairs.add(new BasicNameValuePair("lastname",v2));
            nameValuePairs.add(new BasicNameValuePair("age",v3));
            nameValuePairs.add(new BasicNameValuePair("email_address",v4));
            nameValuePairs.add(new BasicNameValuePair("confirm_email_address",v5));



            StrictMode.setThreadPolicy(policy);

            try
            {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("localhost/Android/Insert.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost); 
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

                Log.e("log_tag", "connection success ");
                Toast.makeText(getApplicationContext(), "Subscription Successfull.Thank You for Subscribing you have now been added to our Mailing Lists", Toast.LENGTH_LONG).show();
            }
            catch(Exception e)
            {
                Log.e("log_tag", "Error in http connection "+e.toString());
                Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
            }
            try
            {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) 
                {
                        sb.append(line + "\n");
                        Intent i = new Intent(getBaseContext(),SecondActivity.class);
                        startActivity(i);
                }
                is.close();

                result=sb.toString();
            }
            catch(Exception e)
            {
               Log.e("log_tag", "Error converting result "+e.toString());
            }
            try
            {   
                    JSONObject json_data = new JSONObject(result);
                    CharSequence w= (CharSequence) json_data.get("result");

                    Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();
            }
            catch(JSONException e)
            {
                    Log.e("log_tag", "Error parsing data "+e.toString());
                    Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
            }
        }


    });

}

    private boolean isEmailValid(String email) {

        return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
    }}
解决方案

Try this way,hope this will help you to solve your problem.

activity_subscribe.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="5dp">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <EditText
                android:id="@+id/edtFirstName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="FirstName"
                android:layout_marginTop="5dp"/>

            <EditText
                android:id="@+id/edtLastName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="LastName"
                android:layout_marginTop="5dp"/>
            <EditText
                android:id="@+id/edtAge"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Age"
                android:layout_marginTop="5dp"/>
            <EditText
                android:id="@+id/edtEmail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Email"
                android:inputType="textEmailAddress"
                android:layout_marginTop="5dp"/>
            <EditText
                android:id="@+id/edtPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:hint="Password"
                android:layout_marginTop="5dp"/>
            <EditText
                android:id="@+id/edtConfirmPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:hint="Confirm Password"
                android:layout_marginTop="5dp"/>
            </LinearLayout>
        </ScrollView>
    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"/>

</LinearLayout>

SubscribeActivty.java

public class SubscribeActivty extends Activity {

    InputStream is = null;
    private EditText edtFirstName;
    private EditText edtLastName;
    private EditText edtAge;
    private EditText edtEmail;
    private EditText edtPassword;
    private EditText edtConfirmPassword;
    private Button btnSubmit;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subscribe);

        edtFirstName = (EditText) findViewById(R.id.edtFirstName);
        edtLastName = (EditText) findViewById(R.id.edtLastName);
        edtAge = (EditText) findViewById(R.id.edtAge);
        edtEmail = (EditText) findViewById(R.id.edtEmail);
        edtPassword = (EditText) findViewById(R.id.edtPassword);
        edtConfirmPassword = (EditText) findViewById(R.id.edtConfirmPassword);
        btnSubmit = (Button) findViewById(R.id.btnSubmit);

        btnSubmit.setOnClickListener(new View.OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(View view) {

                boolean isValid = true;
                if (edtFirstName.getText().toString().length() <= 0) {
                    edtFirstName.setError("Value  Required");
                    isValid = false;
                } else if (!edtFirstName.getText().toString().matches("[a-zA-Z ]+")) {
                    edtFirstName.setError("Accept Alphabets Only.");
                    isValid = false;
                }

                if (edtLastName.getText().toString().length() <= 0) {
                    edtLastName.setError("Value  Required");
                    isValid = false;
                } else if (!edtLastName.getText().toString().matches("[a-zA-Z ]+")) {
                    edtLastName.setError("Accept Alphabets Only.");
                    isValid = false;
                }

                if (edtEmail.getText().toString().length() <= 0) {
                    edtEmail.setError("Value  Required");
                    isValid = false;
                } else if (!isEmailValid(edtEmail.getText().toString())) {
                    edtLastName.setError("Invalid Email.");
                    isValid = false;
                }

                if (edtAge.getText().toString().length() <= 0) {
                    edtAge.setError("Value  Required");
                    isValid = false;
                } else if (!edtAge.getText().toString().matches("[0-9 ]+")) {
                    edtAge.setError("Accept Numbers Only.");
                    isValid = false;
                }

                if (edtPassword.getText().toString().length() <= 0) {
                    edtPassword.setError("Value  Required");
                    isValid = false;
                }

                if (edtConfirmPassword.getText().toString().length() <= 0) {
                    edtConfirmPassword.setError("Value  Required");
                    isValid = false;
                } else if (!edtConfirmPassword.getText().toString().equals(edtPassword.getText().toString())) {
                    edtConfirmPassword.setError("Password Mismatch.");
                    isValid = false;
                }


                if (isValid) {
                    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                    nameValuePairs.add(new BasicNameValuePair("firstname", edtFirstName.getText().toString()));
                    nameValuePairs.add(new BasicNameValuePair("lastname", edtLastName.getText().toString()));
                    nameValuePairs.add(new BasicNameValuePair("age", edtAge.getText().toString()));
                    nameValuePairs.add(new BasicNameValuePair("email", edtEmail.getText().toString()));
                    nameValuePairs.add(new BasicNameValuePair("password", edtPassword.getText().toString()));

                    submitDataOnServer(nameValuePairs);
                }
            }

        });

    }

    public void submitDataOnServer(final ArrayList<NameValuePair> nameValuePairs){
        new AsyncTask<Void,Void,String>(){
            @Override
            protected String doInBackground(Void... params) {
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("localhost/Android/Insert.php");
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();

                    Log.e("log_tag", "connection success ");
                    Toast.makeText(getApplicationContext(), "Subscription Successfull.Thank You for Subscribing you have now been added to our Mailing Lists", Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Log.e("log_tag", "Error in http connection " + e.toString());
                    Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
                }
                try {

                    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    is.close();

                    return sb.toString();
                } catch (Exception e) {
                    Log.e("log_tag", "Error converting result " + e.toString());
                }
                return null;
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                try {
                    JSONObject json_data = new JSONObject(result);
                    CharSequence w = (CharSequence) json_data.get("result");

                    Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();
                } catch (JSONException e) {
                    Log.e("log_tag", "Error parsing data " + e.toString());
                    Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
                }
            }
        }.execute();
    }


    public boolean isEmailValid(final String mailAddress) {
        Pattern pattern;
        Matcher matcher;
        final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        pattern = Pattern.compile(EMAIL_PATTERN);
        matcher = pattern.matcher(mailAddress);
        return matcher.matches();
    }
}

这篇关于EDITTEXT:错误解决再启用机器人发送按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 11:11