Как определить кнопки прямо внутри Java Activity Class? - PullRequest
0 голосов
/ 30 декабря 2018

Я разработал две кнопки b1, b2 внутри "KeyboardService.java", которые должны обновить значение PACK_LIB внутри "Stickers.java".

Прикреплено три файла:

main_board_layout.xml
Stickers.java 
KeyboardService.java   

Я назначил обе кнопки с идентификаторами Button1 = b1 и Button2 = b2.Построение прошло успешно, но когда я нажимаю на кнопки, обе не работают.

Я думаю, что, возможно, проблема с этой строкой ->

final Button button1 = (Button) mainBoard.findViewById(R.id.b1);

Поскольку кнопки находятся внутри FrameLayout,Но когда я использую идентификатор, он тоже не будет работать.

Есть идеи?

------- main_board_layout.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_board"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/black"
    android:orientation="vertical">

    <android.inputmethodservice.KeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/keyboard_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/black" />
    <!--top bar-->
    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#ffffff"/>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:padding="2dp"
        android:id="@+id/buttons"
        >

        <Button
            android:id="@+id/b1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button1" />

        <Button
            android:id="@+id/b2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="@string/button2"

            />

        <TextView
            android:id="@+id/packNameLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:text="@string/app_name"
            android:textColor="#000000"
            android:textSize="1sp"
            android:visibility="gone"
            />

        <ImageView
            android:id="@+id/btShareLinkGP"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:src="@mipmap/bt_share"
            tools:srcCompat="@mipmap/bt_share"
            />


    </FrameLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:background="#000000"/>
    <ScrollView
        android:id="@+id/gif_view"
        android:layout_width="match_parent"
        android:layout_height="190dp"
        android:background="@color/black"
        android:paddingLeft="0dp"
        android:paddingRight="15dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:gravity="left|center"
        android:orientation="horizontal"
        android:padding="0dp">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/pack_recycler_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/black"
            android:orientation="horizontal"
            app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
    </LinearLayout>

</LinearLayout>

------Stickers.java

public static String PACK_LIB ="";

public void setDefaultStickerPack() {
    checkVersion(true);
    InputStream in = null;
    String packList[]=new String[0];
    final String PACK_APP="pack_app";
    final String PACK_ICON="pack_on.png";
    String curAssets="";

------- KeyboardService.java

@Override
    public View onCreateInputView() {
        mainBoard = (LinearLayout) getLayoutInflater().inflate(R.layout.main_board_layout, null);
        packNameLabel = (TextView) mainBoard.findViewById(R.id.packNameLabel);
        scrollView = (ScrollView) mainBoard.findViewById(R.id.gif_view);

        stickerView = (RecyclerView) getLayoutInflater().inflate(R.layout.recycler_view, null);
        stickerView.addItemDecoration(new MarginDecoration(this));
        stickerView.setHasFixedSize(true);
        stickerView.setLayoutManager(new GridLayoutManager(this, 4));

        scrollView.addView(stickerView);

        ImageView btShareLinkGP = (ImageView) mainBoard.findViewById(R.id.btShareLinkGP);
        btShareLinkGP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shareLinkToGP();
            }
        });

        // packs bar
        packView = (RecyclerView) mainBoard.findViewById(R.id.pack_recycler_view);

        // BUTTONS ACTIONS
        final Button button1 = (Button) mainBoard.findViewById(R.id.b1);
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Stickers.PACK_LIB = "allstickers";
            }
        });

        final Button button2 = (Button) mainBoard.findViewById(R.id.b2);
        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Stickers.PACK_LIB = "teststickers";
            }
        });

        showStickers();
        return mainBoard;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...