onClicklistener在片段中不起作用

时间:2015-05-20 17:27:33

标签: java android android-fragments

我的按钮和片段中的onClicklistener有问题。当我运行我的应用程序时,片段出现但按钮不起作用,我没有收到任何错误。请帮我。感谢。

我的片段类(.java):

public class SideFragment extends Fragment {

View view;
final private String TAG = "Side Fragment";

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

    Log.i(TAG, "onCreateView Started!");
    view = inflater.inflate(R.layout.activity_new, container, false);

    Button commentButton = (Button) view.findViewById(R.id.commentBtn);
    commentButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String name, comment, other;

            EditText nameIn = (EditText) view.findViewById(R.id.nameIn);
            EditText commentIn = (EditText) view.findViewById(R.id.commentIn);
            EditText otherIn = (EditText) view.findViewById(R.id.otherIn);

            TextView nameOut = (TextView) view.findViewById(R.id.name);
            TextView commnetOut = (TextView) view.findViewById(R.id.commnet);
            TextView otherOut = (TextView) view.findViewById(R.id.other);

            name = nameIn.getText().toString();
            comment = commentIn.getText().toString();
            other = otherIn.getText().toString();

            nameOut.setText(name);
            commnetOut.setText(comment);
            otherOut.setText(other);
        }
    });

    Log.i(TAG, "Button Clicked!");

    return view;

}//end of onCreateView function
}//end of SideFragment class

activity_new.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="5dp" >

    <EditText
        android:id="@+id/nameIn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="@string/name"
        android:inputType="text" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/commentIn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:ems="10"
        android:hint="@string/comment"
        android:inputType="text" />

    <EditText
        android:id="@+id/otherIn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:ems="10"
        android:hint="@string/other"
        android:inputType="text" />

    <Button
        android:id="@+id/commentBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add_comment"
        android:enabled="true" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="@string/blank"
        android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/commnet"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="@string/blank"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/other"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="@string/blank"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

这是我添加片段

的xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_across"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" >

<LinearLayout
    android:id="@+id/LinearLayout2"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.thinkpad.androidtest.MainActivity" >

    <TextView
        android:id="@+id/ourTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/ourButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_text" />
</LinearLayout>

<fragment
    android:id="@+id/fragment1"
    android:name="com.thinkpad.androidtest.SideFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    tools:layout="@layout/activity_new" />

</LinearLayout>

0 个答案:

没有答案