需要单击两次按钮才能工作-为什么?

时间:2019-01-04 10:40:45

标签: android button onclick fragment onclicklistener

我的问题是我的活动中的Button需要单击两次以使代码执行以onClick方法编写的代码。 当我共享我的活动代码和布局文件代码时。请指导我解决问题。

活动(片段)

public class SelfFamilyInfoActivity extends Fragment {

Button btnUpdateFamily;

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

    View v = inflater.inflate(R.layout.self_mem_details_family_info, 
  container, false);

    try {

        btnUpdateFamily = v.findViewById(R.id.btnUpdateFamily);

        btnUpdateFamily.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(getActivity(), "Button Clicked", 
        Toast.LENGTH_SHORT).show();

            }
        });


    } catch (Exception e) {
        e.printStackTrace();
    }

    return v;

}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="vertical">

<ScrollView

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:scrollbars="vertical">

    <LinearLayout

        android:id="@+id/llLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="12dp"
        android:orientation="vertical">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:gravity="center">

                <Button
                    android:id="@+id/btnUpdateFamily"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:layout_marginBottom="5dp"
                    android:background="@drawable/button_background"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:text="Update Family Info"
                    android:textColor="@color/buttonTextColorWhite"
                    android:textSize="18sp" />

            </LinearLayout>


        </LinearLayout>

 </ScrollView>
 </android.support.v4.widget.NestedScrollView>

在另一个MainActivity中初始化此片段时。

1 个答案:

答案 0 :(得分:1)

对于片段,最好使用<?php namespace App\EventSubscriber; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; use Symfony\Component\Security\Http\SecurityEvents; class LoginEventSubscriber implements EventSubscriberInterface { protected $em; public function __construct(EntityManagerInterface $em) { $this->em = $em; } public static function getSubscribedEvents() { return [ // Subscribe to login event - fire 'storeLastLogin' method SecurityEvents::INTERACTIVE_LOGIN => 'storeLastLogin' ]; } public function storeLastLogin(InteractiveLoginEvent $event) { // Retrieve the User entity from token $user = $event->getAuthenticationToken()->getUser(); // It means that you have a 'lastLogin' property in your User entity $user->setLastLogin(new \DateTime()); $this->em->persist($user); $this->em->flush(); } } 方法中的全视图。可能尚未准备好视图,但您开始单击。并且您使用onViewCreated块在日志中看到任何异常了。我不确定。但是您可以使用try catch方法尝试点击侦听器。

已更新 尝试使用此代码。此代码在我的部分中起作用

onViewCreated
相关问题