改造登录按钮单击不使用片段

时间:2016-01-12 15:20:39

标签: android android-fragments retrofit

我尝试使用retrofit登录,其中来自活动登录的登录按钮会在成功时将我重定向到主页活动。然后我尝试使用片段。我在主活动上有两个可以互相替换的片段,即登录片段和寄存器片段。知道,我知道如果我想使用任何片段的元素,那么我必须实现一个主要活动必须实现的接口才能使用let说的edittext,片段中的按钮。我已经这样做了,但是当我尝试在登录按钮方法中实现改造的异步任务时,当我点击它时,什么也没发生。我尝试在同一个登录按钮方法中放一个吐司,它可以工作,但不是改装调用。

登录片段:

public class LoginLayout extends Fragment {

EditText schoolid;
EditText password;
Login login;
Button loginButton;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.login_layout, container, false);
    schoolid = (EditText) view.findViewById(R.id.loginschoolid);
    password = (EditText) view.findViewById(R.id.loginpassword);
    loginButton = (Button) view.findViewById(R.id.login);
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String schoolidtext = schoolid.getText().toString();
            String passwordtext = password.getText().toString();
            login.loginButtonClicked(schoolidtext, passwordtext);
        }
    });
    return view;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try{
        login = (Login) activity;
    }catch(Exception e) {

    }
}

public interface Login{

    public void loginButtonClicked(String schoolid, String password);

 }
}

MainActivity:

public void loginUser(String schoolid, String password) {
    //Here we will handle the http request to insert user to mysql db
    //Creating a RestAdapter
    RestAdapter adapter = new RestAdapter.Builder()
            .setEndpoint(ROOT_URL) //Setting the Root URL
            .build(); //Finally building the adapter

    //Creating object for our interface
    LoginAPI api = adapter.create(LoginAPI.class);

    api.loginUser(

            //Passing the values
            schoolid,
            password,

            //Creating an anonymous callback
            new Callback<Response>() {
                @Override
                public void success(Response result, Response response) {
                    //On success we will read the server's output using bufferedreader
                    //Creating a bufferedreader object
                    BufferedReader reader = null;

                    //An string to store output from the server
                    String output = "";

                    //Initializing buffered reader
                    try {
                        reader = new BufferedReader(new InputStreamReader(result.getBody().in()));

                        //Reading the output in the string
                        output = reader.readLine();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    if (output.equals("Successful")) {

                        isLoggedIn = true;

                        if(isLoggedIn == true) {
                            Intent i = new Intent(MainActivity.this, HomeActivity.class);
                            startActivity(i);
                        }

                        //Displaying the output as a toast
                        Toast.makeText(MainActivity.this, output, Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void failure(RetrofitError error) {
                    //If any error occured displaying the error as toast
                    Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                }
            }

    );
}

@Override
public void loginButtonClicked(String schoolid, String password) {
    //this line doesn't work  
    loginUser(schoolid, password);
    //this line works
    Toast.makeText(MainActivity.this, "button clicked", Toast.LENGTH_LONG).show();
}

1 个答案:

答案 0 :(得分:1)

片段附加到活动,onFragmentInteraction是您的活动用来与片段交互的回调方法

LoginFragment

创建一个监听器

private OnFragmentInteractionListener mListener;


/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p/>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    void onLoginFragmentInteraction(String schoolidtext, String passwordtext);
}

初始化侦听器

@Override
public void onStart() {
    super.onStart();
    try {
        mListener = (OnFragmentInteractionListener) getActivity();
    } catch (ClassCastException e) {
        throw new ClassCastException(getActivity().toString()
                + " must implement OnFragmentInteractionListener");
    }
}

致电聆听者

loginButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String schoolidtext = schoolid.getText().toString();
        String passwordtext = password.getText().toString();
        //call the listenner on the main activity
        mListener.onLoginFragmentInteraction(schoolidtext, passwordtext);
    }
});

MainActivity

实施回电:

public class MainActivity extends Activity 
                          implements LoginFragment.OnFragmentInteractionListener{

}

覆盖

@Override
public void onLoginFragmentInteraction(String schoolidtext, String passwordtext) {
    loginUser(schoolid, password);
    Toast.makeText(MainActivity.this, "button clicked", Toast.LENGTH_LONG).show();
}