OnClickListener во фрагменте не работает.Намерение от фрагмента к активности - PullRequest
0 голосов
/ 20 сентября 2018

Возможно, это будет тот же или дублирующий вопрос: Переход от одного фрагмента к другому виду деятельности с использованием ListView

Но у меня проблема с переходом от фрагмента к действию.У меня есть основное действие со стандартным блоком навигации сборки, который заменяет мой пользовательский фрагмент фрагментом content_main.В моем фрагменте я хочу установить OnClickListener на TextView, чтобы перейти к другой деятельности.Но это не работает!Я не могу решить эту проблему в течение 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()

Я пытался установить этот слушатель на кнопку для эксперимента, но у меня не было успеха.Затем я попытался настроить свою деятельность на предмет другого навигационного ящика, но он тоже не сработал.

Это просто пустой белый экран!Можете ли вы помочь мне, пожалуйста?

1 Ответ

0 голосов
/ 20 сентября 2018

Почему бы вам не использовать представление кнопок для этого действия ??

Или добавить этот код в ваш TextView xml:

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