Okay so I have this login / register function on a same page. So far everything is working but I am trying to make a check whereby if the user does not enter anything when he click login or register , it will prompt the user a message with a toast like "1 or more field is left empty. please check and try again" . Please advise me . thanks
package mdad.project;
import com.example.manandhowproject.R;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends Activity {
SQLiteDatabase db;
Button btnLogin, btnSignUp;
EditText etUsername, etPassword;
SQLiteOpenHelper dbhelper;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
etUsername = (EditText)findViewById(R.id.etUsername);
etPassword = (EditText)findViewById(R.id.etPassword);
Button button = (Button)findViewById(R.id.btnLogin);
button.setOnClickListener(new OnClickListener(){
public void onClick (View V){
if(login(etUsername.getText().toString(), etPassword.getText().toString()) == 1){
Toast.makeText(getApplicationContext(), "Log In Success ! ", Toast.LENGTH_LONG).show();
Intent msg1 = new Intent(Login.this, Userreg.class);
startActivity(msg1);
}else{
Toast.makeText(getApplicationContext(), "Error Logging In, Please Try Again", Toast.LENGTH_LONG).show();
}
}
});
Button btnSignUp = (Button) findViewById (R.id.btnSignUp);
btnSignUp.setOnClickListener(new OnClickListener (){
public void onClick (View V) {
Toast.makeText(getApplicationContext(), "Sign Up Success ! ", Toast.LENGTH_LONG).show();
String username= etUsername.getText().toString();
String password= etPassword.getText().toString();
String sql = "insert into Registration (Username ,Password) values( '"+username+"','"+password+"')";
String result = updateTable(sql);
etUsername.setText("");
etPassword.setText("");
}});
String sql="create table if not exists Registration (recld integer PRIMARY KEY autoincrement, Username text, Password text)";
String result = createDatabase(sql, "Reg.db");
}
public int login(String username, String password){
String[] selectionArgs = new String[]{username, password};
try
{
int i = 0;
Cursor c = null;
c = db.rawQuery("select * from Registration where username=? and password=?", selectionArgs);
c.moveToFirst();
i = c.getCount();
c.close();
return i;
}
catch(Exception e)
{
e.printStackTrace();
}
return 0;
}
String createDatabase(String sql, String dbName) {
try{
System.out.println(sql);
db = SQLiteDatabase.openOrCreateDatabase("sdcard/" + dbName,null);
db.beginTransaction();
db.execSQL(sql);
db.setTransactionSuccessful();
db.endTransaction();
}
catch (Exception e){
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
System.out.println(e.toString());
return ("error open DB");
}
return "";
}
String updateTable(String sql)
{
try{
System.out.println(sql);
db.beginTransaction();
db.execSQL(sql);
db.setTransactionSuccessful();
db.endTransaction();
}catch (Exception e) { System.out.println(e.toString());
return ("Error updating DB");
}
return ("DB updated");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
you can check the length of the edit texts before they are submitted
as you do the check
if(etUsername.getText().toString(), etPassword.getText().toString()) == 1
before the above line check for blanks like this
if(etUsername.getText().toString().trim().length()==0) {
etUsername.setError("this field should be filled");
return;
} else if(etPassword.getText().toString().trim().length()==0) {
etPassword.setError("this field should be filled");
return;
}
then you can contune what you are already did
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.