单击FB登录按钮时的EditText视图验证

时间:2015-07-09 18:29:23

标签: android android-fragments android-edittext facebook-login

当用户点击FB登录按钮时,我想验证用户在EditText视图中输入的手机号码。没有有效的手机,用户就无法登录。

我在MainActivity的片段中都有我的FB登录按钮和EditText视图。请帮助,我已经尝试了一切。

ContactDetail.java

  public class ContactDetail extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_detail);

    //Code for adding permanent '+91' at starting of Mobile No text field
    final EditText edt = (EditText) findViewById(R.id.edit_text_mobileno);

    //Default text of the field
    edt.setText("+91 ");
    Selection.setSelection(edt.getText(), edt.getText().length());

    edt.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub
        }

        //Adding +91 as permanent text
        @Override
        public void afterTextChanged(Editable s) {
            if (!s.toString().startsWith("+91 ")) {
                edt.setText("+91 ");
                Selection.setSelection(edt.getText(), edt.getText().length());

            }

        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

ContactDetailFragment.java

  public class ContactDetailFragment extends Fragment {

private TextView textDetail;
private CallbackManager mCallbackManager;
private AccessTokenTracker mTracker;
private ProfileTracker mProfileTracker;

private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        AccessToken accessToken = loginResult.getAccessToken();
        Profile profile = Profile.getCurrentProfile();
        displayWelcomeMessage(profile);
    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onError(FacebookException e) {

    }
};

public ContactDetailFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
    mCallbackManager = CallbackManager.Factory.create();
    //Method to track access token for change in token values provided by FB login
    mTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
        }
    };
    mProfileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
            displayWelcomeMessage(newProfile);
        }
    };

    //Started Tracking facebook token for FB-Login
    mTracker.startTracking();
    mProfileTracker.startTracking();

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_contact_detail, container, false);
}

private void displayWelcomeMessage(Profile profile) {
    if (profile != null) {
        textDetail.setText("Welcome" + profile.getName());
    }
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);


    LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
    loginButton.setReadPermissions("user_friends");
    loginButton.setFragment(this);
    loginButton.registerCallback(mCallbackManager, mCallback);
    textDetail = (TextView) view.findViewById(R.id.text_detail);
}

@Override
public void onResume() {
    super.onResume();
    Profile profile = Profile.getCurrentProfile();
    displayWelcomeMessage(profile);
}

//Stopping the tracking of Facebook token on stopping the app
@Override
public void onStop() {
    super.onStop();
    mProfileTracker.stopTracking();
    mTracker.stopTracking();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    mCallbackManager.onActivityResult(requestCode, resultCode, data);
}}

1 个答案:

答案 0 :(得分:0)

Facebook禁用此功能,来源here

即使文章很旧,看起来也不会重新启用。