Android studio 在实现登录方法之前在android中验证登录表单
在尝试调用方法并在mysql中执行查询之前,我需要帮助过滤登录活动中的空edittext。。我是android开发的新手,真的迷路了。。我遵循了一个教程,该教程在注册和登录方面效果很好,但没有过滤或验证。遗憾的是,我无法完全理解每个函数/方法如何运行的步骤。。如果您能给我一个链接,让我找到一个更好的页面,以获得一个不过时或不推荐使用的教程,我将不胜感激。。我使用的是安卓1.5 我一直在这里搜索谷歌和线程,但我找不到一个android新手可以理解的解决方案 下面是处理登录的my Main.java的代码Android studio 在实现登录方法之前在android中验证登录表单,android-studio,Android Studio,在尝试调用方法并在mysql中执行查询之前,我需要帮助过滤登录活动中的空edittext。。我是android开发的新手,真的迷路了。。我遵循了一个教程,该教程在注册和登录方面效果很好,但没有过滤或验证。遗憾的是,我无法完全理解每个函数/方法如何运行的步骤。。如果您能给我一个链接,让我找到一个更好的页面,以获得一个不过时或不推荐使用的教程,我将不胜感激。。我使用的是安卓1.5 我一直在这里搜索谷歌和线程,但我找不到一个android新手可以理解的解决方案 下面是处理登录的my Main.java
public class Main extends AppCompatActivity implements View.OnClickListener {
EditText name, password;
String Name, Password;
Context ctx=this;
String NAME=null, PASSWORD=null, EMAIL=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
name = (EditText) findViewById(R.id.main_name);
password = (EditText) findViewById(R.id.main_password);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void main_register(View v){
startActivity(new Intent(this,Register.class));
}
public void main_login(View v){
Name = name.getText().toString();
Password = password.getText().toString();
BackGround b = new BackGround();
b.execute(Name, Password);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.main_login:
break;
}
}
class BackGround extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String name = params[0];
String password = params[1];
String data="";
int tmp;
try {
URL url = new URL("http://10.0.2.2/BirdBreedingManagement/scripts/login.php");
String urlParams = "name="+name+"&password="+password;
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
os.write(urlParams.getBytes());
os.flush();
os.close();
InputStream is = httpURLConnection.getInputStream();
while((tmp=is.read())!=-1){
data+= (char)tmp;
}
is.close();
httpURLConnection.disconnect();
return data;
} catch (MalformedURLException e) {
e.printStackTrace();
return "Exception: "+e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "Exception: "+e.getMessage();
}
}
@Override
protected void onPostExecute(String s) {
String err=null;
try {
JSONObject root = new JSONObject(s);
JSONObject user_data = root.getJSONObject("user_data");
NAME = user_data.getString("name");
PASSWORD = user_data.getString("password");
EMAIL = user_data.getString("email");
} catch (JSONException e) {
e.printStackTrace();
err = "Exception: "+e.getMessage();
}
Intent i = new Intent(ctx, Home.class);
i.putExtra("name", NAME);
i.putExtra("password", PASSWORD);
i.putExtra("email", EMAIL);
i.putExtra("err", err);
startActivity(i);
}
}
提前感谢。因为在运行
异步任务
从php获取数据之前,您需要检查用户名
和密码
字段是否为空。修改main\u登录(视图v)
方法如下
public void main_login(View v){
Name = name.getText().toString();
Password = password.getText().toString();
if(Name==null || Password==null){
// can alert the user that either one of username or the password is empty by
Toast.makeText(getActivity(), "Username or the Password is empty",Toast.LENGTH_LONG).show();
// in here you can restart the current activity
startActivity(this,Main.class);
finish(); // to destroy the current activity
}
else{
BackGround b = new BackGround();
b.execute(Name, Password);
}
}
Kasun Siyambalapitiya的回答使我能够按照预期执行代码,并进行了一些调整。。我在这里发布它是为了与其他人分享,他们也在寻找一个代码,如果Kasun的答案不符合他们的要求,他们可以尝试
`public void main_login(View v){
Name = name.getText().toString();
Password = password.getText().toString();
if(name.getText().length()==0){
Toast.makeText(getApplicationContext(),
"Please Enter your name", Toast.LENGTH_LONG).show();
Intent intent = getIntent();
startActivity(intent);
}else{
Intent intent = getIntent();
startActivity(intent);
BackGround b = new BackGround();
b.execute(Name, Password);
}
}`
我删除finish命令以避免应用程序关闭和重新打开。谢谢。。它引导我解决我的问题。。我修改了您的代码,因为我无法使这一行工作..Toast.makeText(getActivity(),“用户名或密码为空”,Toast.LENGTH_LONG.show();在
Toast.makeText(getActivity(),“用户名或密码为空”,Toast.LENGTH\u LONG.show()行中,对停止活动进行了一些调整
第一个参数用于将上下文
传递给Toast
。您可以通过this
、getActivity()
和getApplicationContext()
发送context
由于我的答案确实对您有所帮助,因此我很抱歉没有再提它,如果您能投票我的答案或者接受我的答案为正确答案,我将非常高兴。因为目前我无法在这个网站上发布任何问题,因为我已经达到了我当前声誉的最大问题限制,所以我有点急于提高我的声誉以重新排队:)是的,我忘记了,但是你可以通过勾选我答案旁边的正确符号来接受我的正确答案,从这一点上,您还将因接受回答而获得2
声誉。
`public void main_login(View v){
Name = name.getText().toString();
Password = password.getText().toString();
if(name.getText().length()==0){
Toast.makeText(getApplicationContext(),
"Please Enter your name", Toast.LENGTH_LONG).show();
Intent intent = getIntent();
startActivity(intent);
}else{
Intent intent = getIntent();
startActivity(intent);
BackGround b = new BackGround();
b.execute(Name, Password);
}
}`