[英]How to add another activity as a login screen android
i'm new to android and i'm working on an update for my first application. 我是android新手,正在为我的第一个应用程序进行更新。 The update is to put a log in screen in the beginning.
更新是在屏幕的开头放置一个日志。 i chose to use a new activity to do the check for the credentials entered and launch it first then if the username and password match, it'll take the user to the main activity.
我选择使用一个新活动来检查输入的凭据,然后先启动它,然后如果用户名和密码匹配,它将把用户带到主要活动。 i wrote the code, but it's not working.
我写了代码,但是没有用。 When i send it to my phone it gives me the android message error as if it crashed.
当我将其发送到手机时,它给了我android消息错误,好像它崩溃了一样。 I quick heads up, my teacher wants the username and password to be saved as resources string arrays to simulate the database since we didn't learn that yet.
我迅速抬起头来,我的老师希望将用户名和密码另存为资源字符串数组以模拟数据库,因为我们还没有学到。 Other options he suggested is controlling the views, using tabHost, or framLayout, i thought using another activity will be easier since activities are all i have worked with so far.
他建议的其他选项是使用tabHost或framLayout控制视图,我认为使用其他活动会更容易,因为到目前为止我已经使用过所有活动。 If anybody can look at my code and tell me what i did wrong.
如果有人可以看一下我的代码并告诉我我做错了什么。 Also note that i'm not asking to solve my homework for me, i want to learn.
还要注意,我不是要为我解决家庭作业,而是要学习。 Thanks a lot guys.
非常感谢。
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;
}
}
You need to add the right intent filters for your activity in the Manifest xml file. 您需要在清单xml文件中为您的活动添加正确的意图过滤器。
If 'MainActivity' is the name of your activity that you want to start first when the app opens, add the following to the manifest file. 如果“ MainActivity”是您要在应用程序打开时首先开始的活动的名称,则将以下内容添加到清单文件中。 Remember that you have these (ie DEFAULT & LAUNCHER) category fields in the intent-filter only for a single activity in your application.
请记住,您仅在应用程序中的单个活动的意图过滤器中具有这些(即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.