片段onClick方法不起作用

时间:2015-06-22 05:42:04

标签: android android-fragments

我片段中的点击侦听器无法正常工作。我没有从我的requestData方法获取任何调试消息,也没有获取我的onClick方法。我当时认为这可能是因为尝试使用片段中的活动方法,但我没有看到这样做的任何问题;它也不会影响我没有显示的onClick调试消息。

的活动:

public class MainActivity extends AppCompatActivity {

    public void requestData(String first, String second) {
        Log.d(“Success!”, first + “ “ + second);
    }

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

片段:

public class WelcomeFragment extends Fragment implements View.OnClickListener{

    private Button mButton;
    private EditText mFirstEditText;
    private EditText mSecondEditText;

    private View v;

    public WelcomeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        v = inflater.inflate(R.layout.fragment_main, container, false);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        mCalculateButton = (Button) v.findViewById(R.id.calculate_button);
        mCalculateButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        mFirstEditText = (EditText) v.findViewById(R.id.first_editText);
        String first = mFirstEditText.getText().toString();
        Log.d("Button clicked", "mFirstEditText set " + first);

        mSecondEditText = (EditText) v.findViewById(R.id.second_editText);
        String second = mSecondEditText.getText().toString();
        Log.d("Button clicked", "mSecondEditText set " + second);

        v.requestData(first, second);
    }

}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="horizontal">

    <fragment
        android:id="@+id/welcome_fragment"
        android:name="com.example.kent.AnApplication.WelcomeFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</LinearLayout>

welcome_fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivityFragment">

    <TextView
        android:text="@string/welcome "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/welcome_textView"
        android:textSize="20sp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="68dp"/>

    <EditText
        android:layout_width="280dp"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:ems="10"
        android:id="@+id/first_editText"
        android:layout_marginTop="60dp"
        android:layout_below="@+id/welcome_textView"
        android:layout_centerHorizontal="true"/>

    <EditText
        android:layout_width="280dp"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:ems="10"
        android:id="@+id/second_editText"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/calculate_button"
        android:layout_marginTop="43dp"
        android:layout_below="@+id/second_editText"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

3 个答案:

答案 0 :(得分:1)

您没有设置点击的侦听器。请尝试以下代码:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        v = inflater.inflate(R.layout.fragment_main, container, false);
        mButton = (Button) v.findViewById(R.id.your_button_id);
        mButton.setOnClickListener(this);
        return v;
    }

如果您有一些“点击”事件,则必须使用if / else或on on onClick();方法

答案 1 :(得分:0)

将您的Fragment代码更改为以下内容:

    public class WelcomeFragment extends Fragment implements View.OnClickListener{

    private Button mButton;
    private EditText mFirstEditText;
    private EditText mSecondEditText;

    private View v;

    public WelcomeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        v = inflater.inflate(R.layout.fragment_main, container, false);
        return v;
    }

    @Override
    public void onViewCreated (View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mCalculateButton = (Button) view.findViewById(R.id.calculate_button);
        mCalculateButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick (View v) {
               mFirstEditText = (EditText) v.findViewById(R.id.first_editText);
              String first = mFirstEditText.getText().toString();
              Log.d("Button clicked", "mFirstEditText set " + first);

             mSecondEditText = (EditText) v.findViewById(R.id.second_editText);
             String second = mSecondEditText.getText().toString();
             Log.d("Button clicked", "mSecondEditText set " + second);

             v.requestData(first, second);
         }
        });
    }  
 }

答案 2 :(得分:0)

问题在于您的 onclick 方法,您还没有找到发生点击的按钮的ID,请将onclick()方法代码更改为:

 @Override
public void onClick(View view) {
    if(view.getId() == R.id.calculate_button) {
    mFirstEditText = (EditText) v.findViewById(R.id.first_editText);
    String first = mFirstEditText.getText().toString();
    Log.d("Button clicked", "mFirstEditText set " + first);

    mSecondEditText = (EditText) v.findViewById(R.id.second_editText);
    String second = mSecondEditText.getText().toString();
    Log.d("Button clicked", "mSecondEditText set " + second);

    v.requestData(first, second);
}

}

我希望它能为你效劳