所以我有一个简单的任务,检查按钮文本的值是否为空白,如果是,则隐藏按钮。文本框内文本的字符串是(例如Time_A_)(设置为后面的按钮),相应的按钮名称相同,减去下划线(例如Time_A)。我想要做的是简化这段代码,使用像for循环而不是12 if语句。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
mSignInToolbar = (Toolbar) findViewById(R.id.signInToolbar);
setSupportActionBar(mSignInToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mRef = FirebaseDatabase.getInstance().getReference().child("Users");
mRef.keepSynced(true);
mEmailField = (EditText) findViewById(R.id.emailField);
mPasswordField = (EditText) findViewById(R.id.passowrdField);
mSigninBtn = (Button) findViewById(R.id.signinBtn);
mProgress = new ProgressDialog(this);
// Initialize Facebook Login button
FacebookSdk.sdkInitialize(getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.button_facebook_login);
loginButton.setReadPermissions("email", "public_profile");
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
handleFacebookAccessToken(loginResult.getAccessToken());
}
@Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
// ...
}
@Override
public void onError(FacebookException error) {
Log.d(TAG, "facebook:onError", error);
// ...
}
});
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Pass the activity result back to the Facebook SDK
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
private void handleFacebookAccessToken(AccessToken token) {
Log.d(TAG, "handleFacebookAccessToken:" + token);
mProgress.setMessage("Logging in...");
mProgress.show();
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(SignInActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
mProgress.dismiss();
}else{
String uid=task.getResult().getUser().getUid();
String name=task.getResult().getUser().getDisplayName();
String email=task.getResult().getUser().getEmail();
String image=task.getResult().getUser().getPhotoUrl().toString();
DatabaseReference childRef = mRef.child(uid);
childRef.child("name").setValue(name);
childRef.child("email").setValue(email);
childRef.child("image").setValue(image);
mProgress.dismiss();
Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainIntent);
}
}
});
}
答案 0 :(得分:5)
您应该将它们添加到面板,然后遍历面板中的所有控件
foreach(var control in this.panelName.Controls.Where(ctrl => string.IsNullOrEmpty(ctrl.Text))
control.Visibility = Visibility.Hidden;
似乎不需要单独的文本变量。
答案 1 :(得分:0)
我建议在按钮上注册TextChanged
- 事件的事件处理程序,如下所示:
private void button1_TextChanged(object sender, EventArgs e)
{
var button = sender as Button;
button.Visible = (button.Text != String.Empty);
}
这样您就不必更改表单的布局了。