我正在开发一款Android应用。我在一个活动中创建了登录表单,登录后切换到另一个活动。我正在尝试在切换活动时重置EditText字段,但在单击登录按钮时,它会在切换之前显示edittext框。它应该平滑切换,不显示文本框。在我的代码下面,我该怎么做才能做得更好?
package com.mtilab.blogspot.imtiapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import android.content.Intent;
public class MainActivity extends AppCompatActivity {
private TextView messageRef;
private EditText usernameRef, passwordRef;
private Button submitRef, signupRef;
//user and pass for test case
private String user = "admin", pass = "admin";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
messageRef = (TextView)findViewById(R.id.loginTV);
usernameRef = (EditText)findViewById(R.id.usernameET);
passwordRef = (EditText)findViewById(R.id.passwordET);
submitRef = (Button)findViewById(R.id.loginB);
signupRef = (Button)findViewById(R.id.registrationB);
//submitref is ref of login button
submitRef.setOnClickListener(
new View.OnClickListener(){
public void onClick(View vw){
if(usernameRef.getText().toString().equals(user) &&
passwordRef.getText().toString().equals(pass)){
Intent userIntent = new Intent(MainActivity.this, User.class);
startActivity(userIntent);
usernameRef.setText("");
passwordRef.setText("");
messageRef.setText("");
} else{
messageRef.setText("U R Not You!");
passwordRef.setText("");
}
}
}
);
}
// way 2 , when login button pressed
/*public void onClickLoginButton(View view) {
messageRef = (TextView)findViewById(R.id.loginTV);
usernameRef = (EditText)findViewById(R.id.usernameET);
passwordRef = (EditText)findeViewById(R.id.passwordET);
if(usernameRef.getText().toString().equals(user) &&
passwordRef.getText().toString().equals(pass)){
Intent userIntent = new Intent(this, User.class);
startActivity(userIntent);
usernameRef.setText("");
passwordRef.setText("");
messageRef.setText("");
}else{
messageRef.setText("U R Not You!");
passwordRef.setText("");
}
}*/
}
答案 0 :(得分:1)
您可以在开始下一个活动之前隐藏所有视图:
submitRef.setOnClickListener(
new View.OnClickListener(){
public void onClick(View vw){
if(usernameRef.getText().toString().equals(user) &&
passwordRef.getText().toString().equals(pass)){
usernameRef.setVisibility(View.INVISIBLE);
passwordRef.setVisibility(View.INVISIBLE);
messageRef.setVisibility(View.INVISIBLE);
// you might want to keep that
usernameRef.setText("");
passwordRef.setText("");
messageRef.setText("");
Intent userIntent = new Intent(MainActivity.this, User.class);
startActivity(userIntent);
} else{
messageRef.setText("U R Not You!");
passwordRef.setText("");
}
}
}
);
使用View.INVISIBLE
只会隐藏视图,但它仍会占用布局中的空间。如果您不想这样,请使用View.GONE
。
有关详细信息,请参阅documentation。