【问题标题】:OnClickListener in fragment doesn't work. Intent from fragment to activity片段中的 OnClickListener 不起作用。从片段到活动的意图
【发布时间】:2018-09-20 08:13:36
【问题描述】:

也许这将是相同或重复的问题: Moving from One Fragment to another activity Using ListView

但我在从片段跳转到活动时遇到问题。我有标准构建导航抽屉的主要活动,它将我的自定义片段替换为 content_main 片段。在我的片段中,我想在 TextView 上设置 OnClickListener 以跳转到另一个活动。但它不起作用!我不能在 2 天内解决这个问题,希望你能帮助我。

我的片段代码:

public class LoginFragment extends Fragment{

    public LoginFragment() {
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.login_fragment, container, false);

        TextView textView = view.findViewById(R.id.regLink);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), RegistrationActivity.class);
                startActivity(intent);
            }
        });

        return view;
    }
}

login_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg_main"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/logButton"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="@string/login_button_text"
        android:textSize="20sp"
        android:textColor="@color/color_main"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/login_button"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="25dp"
        android:gravity="center_horizontal">

        <TextView
            android:id="@+id/accountCheck"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/account_check_text"
            android:textColor="@color/color_main"
            android:textSize="@dimen/text_size"
            android:layout_margin="5dp"/>

        <TextView
            android:id="@+id/regLink"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sign_up_reference_text"
            android:textColor="@color/color_link"
            android:textSize="@dimen/text_size"
            android:layout_marginTop="5dp" />
    </LinearLayout>

</LinearLayout>

我想对 RegistrationActivity 做出意向:

public class RegistrationActivity extends AppCompatActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.registration_activity);

        Toolbar myToolbar = findViewById(R.id.reg_toolbar);
        setSupportActionBar(myToolbar);
        setTitle("Registration");
    }
}

以及registration_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/bg_main">

    <android.support.v7.widget.Toolbar
        android:id="@+id/reg_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/bg_main"
        android:fitsSystemWindows="true" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="TextView"
        android:textColor="@color/color_main"
        android:textSize="40sp" />

</LinearLayout>

在 Logcat 上我看到下一个错误:

09-20 10:37:42.462 31323-31323/com.alg E/CliptrayUtils: hideClipTrayIfNeeded() TextView is focused!! hideClipTray()

我尝试在实验按钮上设置此侦听器,但没有成功。然后我试图从另一个导航抽屉的项目中确定我的活动,但它也不起作用。

这只是一个空白的屏幕!你能帮帮我吗?

【问题讨论】:

  • 在 RegistrationActivity 中,您覆盖了错误的 onCreate 方法。将onCreate(Bundle savedInstanceState, PersistableBundle persistentState) 替换为protected void onCreate(Bundle savedInstanceState)
  • 你有没有尝试调试问题,添加刹车点,看看发生了什么。
  • 天哪!我很愚蠢..它现在有效!谢谢!!!

标签: android android-fragments android-intent onclicklistener


【解决方案1】:

为什么不使用按钮视图来执行此操作??

或将此代码添加到您的 TextView xml 中:

android:clickable="true"
android:focusable="true"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 2017-02-12
    • 2017-06-15
    • 1970-01-01
    • 2016-02-29
    相关资源
    最近更新 更多