java android登录活动后重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26670227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect after android login activity
提问by QuiteNice
I have created a login activity from this tutorial. But I have no idea how to redirect to my main activity after the login process has succeeded.
我从本教程创建了一个登录活动。但是我不知道如何在登录过程成功后重定向到我的主要活动。
Here is the login.java code:
这是 login.java 代码:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class login extends Activity {
private EditText username=null;
private EditText password=null;
private TextView attempts;
private Button login;
int counter = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
attempts = (TextView)findViewById(R.id.textView5);
attempts.setText(Integer.toString(counter));
login = (Button)findViewById(R.id.button1);
}
public void login(View view){
if(username.getText().toString().equals("admin") &&
password.getText().toString().equals("admin")){
Toast.makeText(getApplicationContext(), "Redirecting...",
Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",
Toast.LENGTH_SHORT).show();
attempts.setBackgroundColor(Color.RED);
counter--;
attempts.setText(Integer.toString(counter));
if(counter==0){
login.setEnabled(false);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
}
Thanks in advance :)
提前致谢 :)
回答by codePG
if(username.getText().toString().equals("admin") &&
password.getText().toString().equals("admin")){
Toast.makeText(getApplicationContext(), "Redirecting...",
Toast.LENGTH_SHORT).show();
Intent i = new Intent(login.this, your_new_activity_name.class);
startActivity(i);
}
Also ensure that the new activity is registered in the AndroidManifest file.
还要确保新活动已在 AndroidManifest 文件中注册。
In the sample code below, change .MainMenu
with your_new_activity_name.
在下面的示例代码中,更改.MainMenu
为 your_new_activity_name。
<activity
android:name=".MainMenu"
android:label="@string/app_name" >
</activity>
This URL will help you learn.
此 URL 将帮助您学习。
http://www.vogella.com/tutorials/AndroidIntent/article.html
http://www.vogella.com/tutorials/AndroidIntent/article.html
I hope it helps!
我希望它有帮助!