我试图在片段内触发一个OnclickListener,以在父活动中执行某些任务。但是由于某种原因,listener方法中的代码无效。吐司和日志均未显示。我不太确定如何将侦听器附加到按钮,因此在实现View.OnclickListener接口时,也许我错过了一些东西。请帮忙。
父母活动:
package app2.tarun.com.fragments2;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AdminPanel extends AppCompatActivity implements
FragmentChangePassword.OnButtonClickedListener{
private String password, c_password;
EditText et_password, et_c_password;
DataBaseHelper db;
Button btn_change_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.admin_panel);
btn_change_password = findViewById (R.id.btn_changePassword);
et_password = findViewById (R.id.admin_input_password);
et_c_password = findViewById (R.id.admin_input_reEnterPassword);
db= new DataBaseHelper (this);
}
public void changeFragment(View view) {
Fragment fragment;
if (view == findViewById (R.id.register)) {
fragment = new FragmentRegisterUser ();
FragmentManager fm = getSupportFragmentManager ();
FragmentTransaction ft = fm.beginTransaction ();
ft.replace (R.id.fragment, fragment);
ft.commit ();
}
if (view == findViewById (R.id.change_password)) {
Log.e("mmmmmmmmmm","ssssssssss");
Toast.makeText (this, "Hello", Toast.LENGTH_LONG).show ();
fragment = new FragmentChangePassword ();
FragmentManager fm = getSupportFragmentManager ();
FragmentTransaction ft = fm.beginTransaction ();
ft.replace (R.id.fragment, fragment);
ft.commit ();
}
}
@Override
public void onButtonClicked() {
Log.e("mmmmmmmmmm","ssssssssss");
Toast.makeText (this, "vdsfds", Toast.LENGTH_LONG).show ();
password = et_password.getText ().toString ();
c_password = et_c_password.getText ().toString ();
if (password.equals (c_password)) {
db.changePassword (password);
finish ();
} else
Toast.makeText (this, "vdsfds", Toast.LENGTH_LONG).show ();
}
}
片段活动:
package app2.tarun.com.fragments2;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class FragmentChangePassword extends Fragment implements View.OnClickListener {
OnButtonClickedListener mCallback;
Button btn_change_password;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_fragment_change_password, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated (view, savedInstanceState);
mCallback = (OnButtonClickedListener) getActivity ();
btn_change_password = getActivity ().findViewById (R.id.btn_changePassword);
}
@Override
public void onClick(View view) {
Log.e("mmmmmmmmmm","ssssssssss");
if(view.getId ()==R.id.btn_changePassword) {
mCallback.onButtonClicked ();
}
}
public interface OnButtonClickedListener {
void onButtonClicked();
}
}
答案 0 :(得分:0)
我不太确定如何将侦听器附加到按钮,因此在实现View.OnclickListener接口时,也许我错过了一些东西
在Fragment上实现View.OnclickListener
时,还需要将侦听器设置为View。
您需要执行以下操作:
btn_change_password.setOnClickListener(this);
this
是指已经实现View.OnclickListener
就您的活动而言,我怀疑您在活动的XML中使用了android:onClick
。因此,当您的XML中包含以下内容时:
<Button
...
android:onClick="changeFragment">
</Button>
您的点击将起作用,因为您在“活动”中使用以下方法:
public void changeFragment(View view) {
...
}