goo blog サービス終了のお知らせ 

タブレット用プログラムの書き止め

android OS & iPadOS の記録。

MyMessageクラスの使い方

2022-10-07 16:52:48 | Android studio 日記

mMessage.isMessageBoard() 開いていると true が戻るので他はタップをスルーさせる。


【MyFragment.java】

public class MyFragment extends Fragment {

    private View.OnClickListener mClick = null;
    private View.OnLongClickListener mLongClick = null;
    private View.OnClickListener mReturnClick = null;

    private MyMessage mMessage;
    // 省略

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 省略

        mMessage = new MyMessage( requireActivity() );


        mClick = v -> {
            if ( mMessage.isMessageBoard() ) return;

            // 処理
        };
        mLongClick = v -> {
            if ( mMessage.isMessageBoard() ) return;

            // 処理
        };

        mReturnClick = v -> {
            if ( mMessage.isMessageBoard() ) return;

            // 処理
        };
        //省略

    }

    // 省略

    @Override
    public void onDestroy() {
        super.onDestroy();

        mMessage.finish();
        mMessage = null;

        mClick = null;
        mLongClick = null;
        mReturnClick = null;
    }
}

 

【myfragment.xml】 【activity_main.xml】複数のFragment で利用表示させるため。

一番下に一文を追加書き込み。

< include layout="@layout/mymessage"/>

 

【mymessage.xml】レイアウトデザイン。

< androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/message_layout"
    android:background="@drawable/message_frame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent">

    < TextView
        android:id="@+id/message_text"
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:maxLines="6"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginTop="50dp"
        android:textSize="@dimen/message_size"
        android:text="@string/message_area"
        android:textColor="@color/black"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    < LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="@dimen/message_margin"
        app:layout_constraintStart_toStartOf="@id/message_text"
        app:layout_constraintEnd_toEndOf="@id/message_text"
        app:layout_constraintTop_toBottomOf="@id/message_text" >

        < TextView
            android:id="@+id/key_done"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="@dimen/message_padding"
            android:paddingLeft="@dimen/message_padding"
            android:background="@drawable/message_button_frame"
            android:textSize="@dimen/message_size"
            android:textColor="@color/black"
            android:text="@string/done" />

        < TextView
            android:id="@+id/key_accepted"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/message_margin"
            android:paddingRight="@dimen/message_padding"
            android:paddingLeft="@dimen/message_padding"
            android:background="@drawable/message_button_frame"
            android:textSize="@dimen/message_size"
            android:textColor="@color/black"
            android:text="@string/accepted"
            tools:ignore="RtlHardcoded" />

        < TextView
            android:id="@+id/key_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/message_margin"
            android:background="@drawable/message_button_frame"
            android:textSize="@dimen/message_size"
            android:textColor="@color/black"
            android:paddingRight="@dimen/message_padding"
            android:paddingLeft="@dimen/message_padding"
            android:text="@string/cancel"
            tools:ignore="RtlHardcoded" />

     </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

 


【message_frame.xml】

< shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <!-- 背景色 -->
    < solid android:color="#E689FBF2" />

    <!-- 角丸 -->
    < corners android:radius="15dp" />

</shape>

【message_button_frame.xml】

< shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <!-- 背景色 -->
    < solid android:color="#36C0FF" />

    <!-- 角丸 -->
    < corners android:radius="15dp" />

</shape>