片段按钮不是交互式的

时间:2016-10-13 10:52:26

标签: java android

所以我的问题是我想在片段中添加按钮的方法。这在其他活动/片段中非常有效,但在这里我无法点击它。然而,我可以启动buttonReference.callOnClick(),它确实有效。只是无法通过在UI中单击它来获得响应。 这段代码看起来非常类似于一个运行良好的代码,所以这是一些超出附加代码的东西,但我会欣赏任何可能出错的提示。

public class PhoneNrFragment extends Fragment {

EditText phoneNr;
TextView areaNr;
Button selectArea;

public PhoneNrFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View view = inflater.inflate(R.layout.phone_nr_fragment, container, false);
    selectArea = (Button)getActivity().findViewById(R.id.phone_nr_frag_choose_area_btn);
    selectArea.setOnClickListener(new View.OnClickListener() {
        @Override
        public  void onClick(View v) {
            Log.v("Login", "PhoneNrFragment on arrow button clicked");
        }
    });
    Log.v("Login", "PhoneNrFragment onCreateView changeArea listener attached");
    phoneNr = (EditText)getActivity().findViewById(R.id.phone_nr_frag_nr_input);
    areaNr = (TextView)getActivity().findViewById(R.id.phone_nr_frag_area_nr);

    return view;
}

//this does work
public void clickDumbButtonByCode() {
    Log.v("Login", "click called from code");
    selectArea.callOnClick();
}

}

//这是我正在使用此片段的活动:     PhoneNrFragment phoneNrFragment;

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

    //SIGN_IN
    Button signInBtn = (Button)findViewById(R.id.login_sign_in_btn);
    signInBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(LOG_TAG, "onClick signInBtn");
            Toast.makeText(getBaseContext(), "onClickSignIn() not implemented", Toast.LENGTH_LONG).show();
        }
    });
    //SIGN_UP
    Button signUpBtn = (Button)findViewById(R.id.login_sign_up_btn);
    signUpBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public  void  onClick(View v) {
            Log.d(LOG_TAG, "onClick signUpBtn");
            phoneNrFragment.clickDumbButtonByCode();
            Toast.makeText(getBaseContext(), "onClickSignUp() not implemented", Toast.LENGTH_LONG);
        }
    });
    //FORGET_PASSWORD
    Button forgottenPassBtn = (Button)findViewById(R.id.remind_password_btn);
    forgottenPassBtn.setOnClickListener(new View.OnClickListener() {
       @Override
        public  void  onClick(View v) {
           Log.d(LOG_TAG, "onClick forgottenPassBtn");
           Toast.makeText(getBaseContext(), "onClickFrogottenPassword", Toast.LENGTH_LONG);
           SplashActivity.SwitchView(getBaseContext(), LostPasswordActivity.class);
       }
    });
    //PHONE_NR_FRAGMENT
    FragmentTransaction fragInjection = getFragmentManager().beginTransaction();
    phoneNrFragment = new PhoneNrFragment();
    fragInjection.replace( R.id.login_phone_nr_fragment, phoneNrFragment).commit();
}
}
//there is nothing more in it except stored reference to this fragment

这是片段布局:               

<!-- AREA_CHOOSE_LINE -->
<LinearLayout
    android:id="@+id/phone_nr_frag_area_line"
    android:orientation="horizontal"
    android:layout_width="280dp"
    android:layout_above="@+id/phone_nr_frag_nr_line"
    android:layout_alignLeft="@+id/phone_nr_frag_nr_line"
    android:layout_alignStart="@+id/phone_nr_frag_nr_line"
    android:layout_marginBottom="8dp"
    android:layout_height="27dp"
    android:layout_centerHorizontal="true">

    <TextView
        android:text="Hong Kong"
        android:layout_height="wrap_content"
        android:id="@+id/phone_nr_frag_area_name"
        tools:text="Select Area"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:textAlignment="viewStart"
        android:layout_gravity="start"
        android:layout_width="243dp"
        android:textSize="17dp"/>

    <Button
        android:layout_width="27dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/phone_nr_frag_choose_area_btn"
        android:background="@drawable/black_right_arrow"/>
</LinearLayout>

<!-- PHONE_NR_LINE -->
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="280dp"
    android:layout_height="50dp"
    android:id="@+id/phone_nr_frag_nr_line"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="8dp"
    android:layout_centerHorizontal="true">
    <!-- area_number -->
    <TextView
        android:layout_height="match_parent"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/phone_nr_frag_area_nr"
        android:layout_weight="1"
        tools:background="@drawable/login_short_input_field"
        android:layout_width="144dp"
        android:textSize="25dp"
        android:gravity="center"/>
    <!-- phone_number -->
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:inputType="phone"
        android:ems="10"
        android:id="@+id/phone_nr_frag_nr_input"
        android:layout_weight="1"
        android:background="@drawable/login_phone_field"
        android:paddingLeft="26dp"
        android:textSize="25dp"/>
</LinearLayout>
</RelativeLayout>

我做错了什么。请记住,从代码中调用此按钮确实有效。我对Android很新,所以我可能会做一些明显错误的事情。 非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

relpace selectArea = (Button)getActivity().findViewById(R.id.phone_nr_frag_choose_area_btn);

selectArea = (Button) view.findViewById(R.id.phone_nr_frag_choose_area_btn);