我正在开发一个有登录页面的应用程序。如果用户成功登录,则“登录”按钮将更改为“注销”。为此我使用login.setText("Logout");
。但是我想为这个按钮改变动作。这意味着如果用户在登录文本更改之前单击登录,则页面将重定向到登录页面。我不知道如何在按钮文本更改后更改操作。请给我一些线索......
答案 0 :(得分:1)
// Try this way,hope this will help you to solve your problem...
btnLoginLogout.setText("Login");
btnLoginLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(((Button)v).getText().equals("Login")){
((Button)v).setText("Logout");
// do your login code here
}else{
((Button)v).setText("Login");
// do your login code here
}
}
});
答案 1 :(得分:1)
您可以尝试这样的事情:
public class Test extends Activity implements View.OnClickListener{
private boolean isLogin = true;
private static final String TEXT_LOGIN = "Login", TEXT_LOGOUT = "Logout";
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_lauyout);
final Button btnLoginLogout = (Button) findViewById(R.id.btn_login_logout);
// Initialize the button with Login text
btnLoginLogout.setText(TEXT_LOGIN);
// Set the click listener for the button
btnLoginLogout.setOnClickListener(this);
}
@Override
public void onClick (View v) {
switch (v.getId()){
case R.id.btn_login_logout:
if (isLogin) {
// Login action
((Button)v).setText(TEXT_LOGOUT);
isLogin = false;
} else {
// Logout action
((Button)v).setText(TEXT_LOGIN);
isLogin = true;
// Redirect here or do an custom action
}
break;
}
}
}
答案 2 :(得分:0)
在你的登录按钮onClick Listener上 试试这种方式
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(login.getText().toString().equals("Login")){
doLogin(); //login functionality here
}else{
doLogout();//logout functionality here
}
}});
答案 3 :(得分:0)
一个简单的解决方案是
logOutButton.setOnClickListener(new OnClickListener()){
@Override
public void onClick(View view){
String buttonText = logOutButton.getText().toString();
if(buttonText.equalsIgnoreCase("Login")){
//let the user login, and change to logout after
}else if(buttonText.equalsIgnoreCase("Logout")){
//let the user logout and change to login after
}
}
});
答案 4 :(得分:0)
@Override
public void onClick(View view){
String buttonText = logOutButton.getText().toString();
if(buttonText.equalsIgnoreCase("Login")){
// logIn code here
view.setText("Logout");
}else{
// logOut code here
view.setText("Login");
}
}