我已经从 this tutorial 创建了一个登录 Activity 。 但我不知道登录过程成功后如何重定向到我的主要 Activity 。
这是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;
}
}
提前致谢:)
最佳答案
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);
}
还要确保新 Activity 已在 AndroidManifest 文件中注册。
在下面的示例代码中,将 .MainMenu
更改为 your_new_activity_name。
<activity
android:name=".MainMenu"
android:label="@string/app_name" >
</activity>
此网址将帮助您学习。
http://www.vogella.com/tutorials/AndroidIntent/article.html
希望对您有帮助!
关于java - Android 登录 Activity 后重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26670227/