android-fragments 在android中验证注册表单

cnjp1d6j  于 2022-11-13  发布在  Android
关注(0)|答案(2)|浏览(185)

我已经创建了一个注册页面,其中包括姓名、地址、PIN码、年龄等。我希望年龄和piicode字段应限制为数字。我曾尝试使用**android:inputType=“number”或android:inputType=“phone:“**但它不工作。请给予任何建议。
Reigistration.java

package com.example.app;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class Registration1 extends Activity  implements OnClickListener,
OnItemSelectedListener{

    private Button mSubmit;
    private Button mCancel;
    private EditText mGname;
    private EditText mFname;
    private EditText mMname;
    private EditText mAddress1;
    private EditText mAddress2;
    private EditText mCityvillage;
    private EditText mStateprovince;
    private EditText mCountry;
    private EditText mPostalcode;
    private EditText mAge;
    private EditText mBirthdate;
    private Spinner mGender;
    private EditText mUsername;
    private EditText mpass;

    private String Gen;

    protected static DBHelper1 DB1;
    String regexStr = "^[0-9]$";
    String regexStr1 ="\\d{3}";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registration1);

        // Assignment of UI fields to the variables
        mSubmit = (Button) findViewById(R.id.submit);
        mSubmit.setOnClickListener(this);

        mCancel = (Button) findViewById(R.id.cancel);
        mCancel.setOnClickListener(this);

        mGname = (EditText) findViewById(R.id.egname);
        mFname = (EditText) findViewById(R.id.efname);
        mMname = (EditText) findViewById(R.id.eMname);
        mAddress1 = (EditText) findViewById(R.id.eaddress1);
        mAddress2 = (EditText) findViewById(R.id.eaddress2);
        mCityvillage = (EditText) findViewById(R.id.ecityvillage);
        mStateprovince = (EditText) findViewById(R.id.estateprovince);
        mCountry = (EditText) findViewById(R.id.ecountry);
        mPostalcode = (EditText) findViewById(R.id.epostalcode);
        mAge = (EditText) findViewById(R.id.eage);
        mBirthdate = (EditText) findViewById(R.id.ebirthdate);
        mGender = (Spinner) findViewById(R.id.spinner1);
        mUsername = (EditText) findViewById(R.id.eusername);
        mpass = (EditText) findViewById(R.id.epass);

        // Spinner method to read the on selected value
        ArrayAdapter<State> spinnerArrayAdapter = new ArrayAdapter<State>(this,
                android.R.layout.simple_spinner_item, new State[] {
                        new State("Male"), new State("Female") });
        mGender.setAdapter(spinnerArrayAdapter);
        mGender.setOnItemSelectedListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

        case R.id.cancel:
            Intent i = new Intent(getBaseContext(), MainActivity.class);
            startActivity(i);
            // finish();
            break;

        case R.id.submit:
            System.out.println("rEGISTER BUTTON CLICK");
            String gname = mGname.getText().toString();
            String fname = mFname.getText().toString();
            String mname = mMname.getText().toString();
            String address1 = mAddress1.getText().toString();

            String address2 = mAddress2.getText().toString();
            String cityvillage = mCityvillage.getText().toString();
            String stateprovince = mStateprovince.getText().toString();
            String country = mCountry.getText().toString();

            String postalcode = mPostalcode.getText().toString();
            String age = mAge.getText().toString();
            String birthdate = mBirthdate.getText().toString();
            Gen = mGender.getSelectedItem().toString();
            String username = mUsername.getText().toString();
            String password = mpass.getText().toString();

            boolean invalid = false;

            if (gname.equals("")) {
                invalid = true;
                Toast.makeText(getApplicationContext(), "Enter your Givenname",
                        Toast.LENGTH_SHORT).show();
            } else

            if (fname.equals("")) {
                invalid = true;
                Toast.makeText(getApplicationContext(),
                        "Please enter your firstname", Toast.LENGTH_SHORT)
                        .show();
            } else

            if (mname.equals("")) {
                invalid = true;
                Toast.makeText(getApplicationContext(),
                        "Please enter your middlename", Toast.LENGTH_SHORT)
                        .show();
            } else

            if (address1.equals("")) {
                invalid = true;
                Toast.makeText(getApplicationContext(),
                        "Please enter your address1", Toast.LENGTH_SHORT)
                        .show();

            } else if (address2.equals("")) {
                invalid = true;
                Toast.makeText(getApplicationContext(),
                        "Please enter your address2", Toast.LENGTH_SHORT)
                        .show();
            } else if (cityvillage.equals("")) {
                invalid = true;
                Toast.makeText(getApplicationContext(),
                        "Please enter your cityvillage", Toast.LENGTH_SHORT).show();
            } else if (stateprovince.equals("")) {
                invalid = true;
                Toast.makeText(getApplicationContext(),
                        "Please enter your stateprovince", Toast.LENGTH_SHORT).show();
            } else if (country.equals("")) {
                invalid = true;
                Toast.makeText(getApplicationContext(),
                        "Please enter your country", Toast.LENGTH_SHORT).show();
            } 
            else if(postalcode.equals("")||postalcode.length()!=7||postalcode.matches(regexStr)==true)
            {
                Toast.makeText(getApplicationContext(),
                        "Please enter valid postal code", Toast.LENGTH_SHORT).show();
            }/*else if (postalcode.equals("")||postalcode.length() != 7) {
            }
                invalid = true;
                Toast.makeText(getApplicationContext(),
                        "Please enter your postalcode Or postalcode must be 7 numbers", Toast.LENGTH_SHORT).show();
                 if(postalcode.length() != 7)
                 {
                       Toast.makeText(getApplicationContext(), "postalcode must be 7 numbers", Toast.LENGTH_SHORT).show();

                 }
            } */else if (age.equals("")||age.matches(regexStr1)==true) {
                //invalid = true;
                Toast.makeText(getApplicationContext(),
                        "Please enter valid age", Toast.LENGTH_SHORT).show();
            }else if (birthdate.equals("")) {
                invalid = true;
                Toast.makeText(getApplicationContext(),
                        "Please enter your birthdate", Toast.LENGTH_SHORT).show();
            }else if (username.equals("")) {
                invalid = true;
                Toast.makeText(getApplicationContext(),
                        "Please enter your Username", Toast.LENGTH_SHORT).show();
            } else if (password.equals("")||!(password.length() >= 6)) {
                invalid = true;
                Toast.makeText(getApplicationContext(),
                        "Password must be at least 6 characters", Toast.LENGTH_SHORT).show();
            } 
            else if (invalid == false) 
            {
                addEntry(gname, fname, mname, address1, address2, cityvillage,
                        stateprovince, country, postalcode, age, birthdate, Gen,
                        username,password);
                Intent i_register = new Intent(Registration1.this,
                        LoginActivity1.class);
                startActivity(i_register);
                // finish();
            }

            break;
        }
    }

    public void onDestroy() {
        super.onDestroy();
        //DB1.close();
    }

    private void addEntry(String gname,String fname,String mname,String address1,String address2,String cityvillage,String
            stateprovince,String country,String postalcode,String age,String birthdate,String Gen,String username,String password) 
    {

        SQLiteDatabase db1 = DB1.getWritableDatabase();
        ContentValues values = new ContentValues();
        //values.put("id", 2);
        values.put("givenname", gname);
        values.put("firstname", fname);
        values.put("middlename", mname);
        values.put("address1", address1);
        values.put("address2", address2);
        values.put("cityvillage", cityvillage);
        values.put("stateprovince", stateprovince);
        values.put("country", country);
        values.put("postalcode", postalcode);
        values.put("age", age);
        values.put("birthdate", birthdate);
        values.put("gender", Gen);
        values.put("username", username);
        values.put("password", password);

        try {
            long rowId = db1.insert(DBHelper1.DATABASE_TABLE_NAME1, null, values);
            System.out.println("rowId: "+rowId);
            Toast.makeText(getApplicationContext(),
                    "your details submitted Successfully...",
                    Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onItemSelected(AdapterView<?> parent, View view, int position,
            long id) {
        // Get the currently selected State object from the spinner
        State st = (State) mGender.getSelectedItem();

        // Show it via a toast
        toastState("onItemSelected", st);
    }

    public void toastState(String name, State st) {
        if (st != null) {
            Gen = st.name;
            // Toast.makeText(getBaseContext(), Gen, Toast.LENGTH_SHORT).show();

        }

    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

    public static void setDB(DBHelper1 dB2) {
        DB1=dB2;

    }



}

registration.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/gname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="Given Name"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/egname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:singleLine="true" >

            <requestFocus />
        </EditText>

        <TextView
            android:id="@+id/fname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="Family Name"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/efname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:singleLine="true" />

            <TextView
            android:id="@+id/mname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="Middle Name"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/eMname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp" />
        <TextView
            android:id="@+id/address1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="Address1"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/eaddress1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp" />
        <TextView
            android:id="@+id/address2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="Address2"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/eaddress2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp" />

        <TextView
            android:id="@+id/cityvillage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="City Village"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/ecityvillage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp" />

        <TextView
            android:id="@+id/stateprovince"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="State Province"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/estateprovince"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp" />

        <TextView
            android:id="@+id/country"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="Country"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/ecountry"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp" />

        <TextView
            android:id="@+id/postalcode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="Postal Code"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/epostalcode"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:inputType="number"/>

         <TextView
            android:id="@+id/age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="Age"
            android:textAppearance="?android:attr/textAppearanceLarge" />
         <EditText
            android:id="@+id/eage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:inputType="number"/>

          <TextView
            android:id="@+id/birthdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="BirthDate"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    <EditText
            android:id="@+id/ebirthdate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp" />
    <TextView
            android:id="@+id/gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="Gender"
            android:textAppearance="?android:attr/textAppearanceLarge" />
        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="User Name"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/eusername"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"/>

        <TextView
            android:id="@+id/pass"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="Password"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/epass"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:inputType="textPassword" />

        <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/submit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:text="Submit" />

            <Button
                android:id="@+id/cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/submit"
                android:text="Cancel" />
        </RelativeLayout>
    </LinearLayout>

</ScrollView>
q8l4jmvw

q8l4jmvw1#

请尝试以下操作:

mPostalcode = (EditText) findViewById(R.id.epostalcode);
mPostalcode.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);

或尝试使用xml:

android:numeric="integer"
sshcrbum

sshcrbum2#

postalcodeEDT.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP){

            // Do what you want
            // (Check if 1.number, 2. Number within range
            return true;
        }
        return false;
    }
});

另外,将以下内容添加到邮政编码字段的xml文件中:

android:inputType="numberDecimal"

编辑:

对于在编辑框中检查年龄,您可能有一个onfocuschangelistener,如下所示:

ageEDT.setOnFocusChangeListener(new OnFocusChangeListener() {

public void onFocusChange(View v, boolean hasFocus) {
    if (!hasFocus) {
        EditText t = (EditText) v;
        if (t.getText() != null
                && !(t.getText().toString()).isEmpty()) {
            try {
                    // Allows age to be floating point too.
                Float i = Float.parseFloat(t.getText().toString());
                if (i >= 100 && i <=5) { 
                            // If age > 100 and <5. 
                    t.setText("");
                    t.setFocusable(true);
                    AlertUserforFocus(
                            "Age",
                            "Invalid Value. Please enter between 5 to 100 !!\n",
                            t);
                }
            } catch (NumberFormatException nfe) {
                // If characters other than numbers is entered
                t.setText("");
                AlertUserforFocus(
                        "Age",
                        "Invalid Value. Please enter between 5 to 100 !!\n",
                        t);
            }
        }
    }
}

});

备注:

AlertUserforFocus(String title,String message,final EditText t)是一个自定义的方法,用于向用户显示一个警告对话框。您可以根据自己的需要显示吐司或其他内容。

相关问题