android按钮单击不在scrollview内部触发

时间:2016-11-07 16:31:40

标签: android button scrollview listener

我在scrollview中有一个按钮有问题,这是我的xml,

<ScrollView
    tools:context="com.francescovenica.remember.fragments.AddAlarmFragment"
    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">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:text="Salva"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginBottom="20dp"
            android:id="@+id/saveAlarm"/>
    </LinearLayout>
</ScrollView>

这里我实例化了按钮(我正在使用片段)

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

        View view = inflater.inflate(R.layout.fragment_add_alarm, container, false);

        btn_saveAlarm = (Button)view.findViewById(R.id.saveAlarm);

        System.out.println("saveAlarm: " + btn_saveAlarm);

        return view;
    }

虽然这是我的倾听者

btn_saveAlarm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("click");
            }
        });

点击不是火,错误在哪里?

编辑-----

我发现了问题,但我不明白......

我在activityCreated中调用setUpView

public void setUpView(){

    btn_saveAlarm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            saveAlarm();
        }
    });

alarm_date = Calendar.getInstance();


    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {

        @Override
        public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {

            alarm_date.set(Calendar.YEAR,year);
            alarm_date.set(Calendar.MONTH,(month+1));
            alarm_date.set(Calendar.DAY_OF_MONTH,dayOfMonth);

            setTimePickerDialog();

        }
    });
}

我不知道为什么但是使用alarm_date = Calendar.getInstance();按钮不起作用,如果我评论它按钮工作正常....为什么Calendar.getInstance打破所有?

1 个答案:

答案 0 :(得分:0)

<ScrollView
    tools:context="com.francescovenica.remember.fragments.AddAlarmFragment"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:clickable="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<!-- rest of code -->

android:clickable="true"应该做的伎俩