简体   繁体   English

片段中的OnClickListener不起作用。 从片段到活动的意图

[英]OnClickListener in fragment doesn't work. Intent from fragment to activity

Maybe it will be the same or duplicating question of this: Moving from One Fragment to another activity Using ListView 可能是相同或重复的问题: 使用ListView从一个片段移动到另一个活动

But i have a problem on jumping from fragment to activity. 但是我在从片段过渡到活动时遇到问题。 I have the main activity with standard build navigation drawer which replaces my custom fragment with content_main fragment. 我的主要活动是使用标准构建导航抽屉,该抽屉将我的自定义片段替换为content_main片段。 In my fragment I want to set OnClickListener on TextView to jump to another activity. 在我的片段中,我想在TextView上设置OnClickListener以跳转到另一个活动。 But it doesn't work! 但这不起作用! I can't solve this problem in 2 days and hope you'll help me. 我无法在2天之内解决此问题,希望您能帮助我。

my code of fragment: 我的片段代码:

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 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>

I want to make intent to the 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");
    }
}

and the registration_activity.xml: 和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>

On the Logcat i see the next error: 在Logcat上,我看到下一个错误:

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

I tried to set this listener on the button for the experiment, but I did't have the success. 我尝试在实验按钮上设置此侦听器,但没有成功。 Then i tried to make intent to my activity from the other nav drawer's item, but it didn't work too. 然后,我试图从另一个导航抽屉中的物品开始我的活动,但是它也没有用。

It's just an empty white screen! 这只是一个空白的屏幕! Can you help me please? 你能帮我吗?

Why don't you use Button view for this action ?? 为什么不使用按钮视图执行此操作?

Or add this code in your TextView xml: 或在您的TextView xml中添加以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM