Android -signup按钮对点击没有影响

时间:2015-01-19 04:43:07

标签: android button

我在一个简单的登录注册应用程序中遇到问题。我刚刚开始在android中编程。我的问题是,注册按钮对点击没有影响。我不知道问题是if else还是数据库。

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;

2 个答案:

答案 0 :(得分:1)

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

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();
                    }   

            }

答案 1 :(得分:0)

这可以通过创建包含按钮状态列表的可绘制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">

              

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