繁体   English   中英

Android -signup按钮对单击无效

[英]Android -signup button has no effect on click

我在一个简单的登录注册应用程序中遇到问题。 我刚刚开始在android中编程。 我的问题是,注册按钮对单击没有作用。 我不知道问题是否出在其他方面还是数据库方面。

signup.java

    public class signup extends Activity{
    Button btn;
    EditText edt1,edt2,edt3,edt4,edt5,edt6,edt7,edt8;
    SQLiteDatabase mydb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);
        edt1=(EditText)findViewById(R.id.nameedit);
        edt2=(EditText)findViewById(R.id.addrsedit);
        edt3=(EditText)findViewById(R.id.cityedit);
        edt4=(EditText)findViewById(R.id.pincodedit);
        edt5=(EditText)findViewById(R.id.usrnmeedit);
        edt6=(EditText)findViewById(R.id.passsgnedit);
        edt7=(EditText)findViewById(R.id.mobedit);
        edt8=(EditText)findViewById(R.id.emledit);
        btn=(Button)findViewById(R.id.sgnup);
        mydb=this.openOrCreateDatabase("shopping",MODE_PRIVATE,null);
        mydb.execSQL("CREATE TABLE IF NOT EXISTS contacts(name varchar,adrs varchar,city varchar,pin varchar,uname varchar,pass varchar,mob varchar,eid varchar)");

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String nm=edt1.getText().toString().trim();
                String ad=edt2.getText().toString().trim();
                String cty=edt3.getText().toString().trim();
                String pin=edt4.getText().toString().trim();
                String usr=edt5.getText().toString().trim();
                String pwd=edt6.getText().toString().trim();
                String mbl=edt7.getText().toString().trim();
                String eml=edt8.getText().toString().trim();            
                Cursor cur=mydb.rawQuery("select * from contacts",null);
                while (cur.moveToNext()) 
                {
                String un=cur.getString(cur.getColumnIndex("uname"));
                String emailPattern="[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";


                if (un.equals(usr))

                    Toast.makeText(signup.this,"username already exits,try another",Toast.LENGTH_SHORT).show(); 


                else{
                         if (eml.matches(emailPattern))
                         {

                            mydb.execSQL("INSERT INTO contacts VALUES('"+nm+"','"+ad+"','"+cty+"','"+pin+"','"+usr+"','"+pwd+"','"+mbl+"','"+eml+"')");
                            Toast.makeText(signup.this, "submitted successfully",500).show();
                            Intent in=new Intent(signup.this,login.class);
                            startActivity(in);
                         }
                         else 
                        {
                            Toast.makeText(getApplicationContext(),"Invalid email address",Toast.LENGTH_SHORT).show();
                        }   
            }
                }

                }

        });

    }

}

EDITED

Cursor cur=mydb.rawQuery("select * from contacts",null);


            Boolean found=false;

            while (cur.moveToNext()) 
            {
                found=true;

            String un=cur.getString(cur.getColumnIndex("uname"));




            if (un.equals(usr))

                Toast.makeText(signup.this,"username already exits,try another",Toast.LENGTH_SHORT).show(); 


            else{
                     if (eml.matches(emailPattern))
                     {

                        mydb.execSQL("INSERT INTO contacts VALUES('"+nm+"','"+ad+"','"+cty+"','"+pin+"','"+usr+"','"+pwd+"','"+mbl+"','"+eml+"')");
                        Toast.makeText(signup.this, "submitted successfully",500).show();
                        Intent in=new Intent(signup.this,MainActivity.class);
                        startActivity(in);
                     }
                     else 
                    {
                        Toast.makeText(getApplicationContext(),"Invalid email address",Toast.LENGTH_SHORT).show();
                    }   

            }
            break;
            }
            if (found) 
                return;

我认为最初您的查询不会返回任何结果。 这可能意味着您的表是空的。

Cursor cur=mydb.rawQuery("select * from contacts",null);
// if table is empty the while loop will fail
while (cur.moveToNext()) 

要测试您的代码,请首先在联系人表中插入一些数据。

尝试这个:

           boolean found = false;
            while(cur.moveToNext()) 
            {
               ... your code like before
               // if found save data and break
               found = true;
               break;
            }

            if(found) return;

                if (eml.matches(emailPattern))
                     {

                        mydb.execSQL("INSERT INTO contacts VALUES('"+nm+"','"+ad+"','"+cty+"','"+pin+"','"+usr+"','"+pwd+"','"+mbl+"','"+eml+"')");
                        Toast.makeText(signup.this, "submitted successfully",500).show();
                        Intent in=new Intent(signup.this,login.class);
                        startActivity(in);
                     }
                     else 
                    {
                        Toast.makeText(getApplicationContext(),"Invalid email address",Toast.LENGTH_SHORT).show();
                    }   

            }

这可以通过创建一个包含按钮状态列表的可绘制xml文件来实现。 因此,例如,如果使用以下代码创建一个名为“ button.xml”的新xml文件:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/YOURIMAGE" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/gradient" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/gradient" />
<item android:drawable="@drawable/YOURIMAGE" />
</selector>

要使背景图像在印刷时保持深色,请创建另一个xml文件,并使用以下代码将其命名为gradient.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<bitmap android:src="@drawable/YOURIMAGE"/>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

机器人:背景=“@绘制/键”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM