[英]How to add another activity as a login screen android
我是android新手,正在为我的第一个应用程序进行更新。 更新是在屏幕的开头放置一个日志。 我选择使用一个新活动来检查输入的凭据,然后先启动它,然后如果用户名和密码匹配,它将把用户带到主要活动。 我写了代码,但是没有用。 当我将其发送到手机时,它给了我android消息错误,好像它崩溃了一样。 我迅速抬起头来,我的老师希望将用户名和密码另存为资源字符串数组以模拟数据库,因为我们还没有学到。 他建议的其他选项是使用tabHost或framLayout控制视图,我认为使用其他活动会更容易,因为到目前为止我已经使用过所有活动。 如果有人可以看一下我的代码并告诉我我做错了什么。 还要注意,我不是要为我解决家庭作业,而是要学习。 非常感谢。
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// buttons variables
Button signIn, exit , clear;
final HashMap<String, String> hash= new HashMap<String, String>();
hash.put(getString(R.array.usernameArray),getString(R.array.passwordArray));
// text boxes variables
final EditText userName = (EditText) findViewById(R.id.usernameEditText);
userName.setEnabled(true);
final EditText password = (EditText) findViewById(R.id.passwordEditText);
password.setEnabled(true);
// initializing the signin button
signIn = (Button) findViewById(R.id.signInButton);
signIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int count = 3;
EditText usrnm = (EditText)findViewById(R.id.usernameEditText);
EditText pswd = (EditText)findViewById(R.id.passwordEditText);
if(hash.containsKey(usrnm))
{
String val = hash.get(usrnm);
if(val.equals(pswd)){
Toast.makeText(getApplicationContext(),
"the values are matched",
Toast.LENGTH_LONG).show();
Intent openMainActivity = new Intent("com.alijaouhari.paycalculator.MainActivity");
startActivity(openMainActivity);
}
else if(count>0)
{
Toast.makeText(getApplicationContext(),
"Wrong Credentials, Please check your usename and password and try again",
Toast.LENGTH_LONG).show();
count--;
}
}
}
}); // end signin button
// initializing the exit button
exit = (Button) findViewById(R.id.exitButton);
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
}); // end exit button
// initializing the clear button
clear = (Button) findViewById(R.id.clearButton);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// when clicking the clear button, it sets all the text edit boxes to an empty string
userName.setText("");
password.setText("");
}
});// end clear button
}
@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;
}
}
您需要在清单xml文件中为您的活动添加正确的意图过滤器。
如果“ MainActivity”是您要在应用程序打开时首先开始的活动的名称,则将以下内容添加到清单文件中。 请记住,您仅在应用程序中的单个活动的意图过滤器中具有这些(即DEFAULT&LAUNCHER)类别字段。
<activity
android:name="com.example.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.