0

我在移动应用程序(android)中工作并开始学习光刻框架,我为组件内的特定小部件编写了一个点击处理程序。但是显示错误

“Builder不能应用于void”为什么会出现这个问题?

如何解决这个问题。我需要另一个问题。如何检索文本输入值 感谢您的回复

package com.hotfoot.rapid.aubank.sbl.fragment.qde;

import android.graphics.Color;
import android.os.Build;
import android.text.InputType;
import android.text.Layout;

import androidx.annotation.RequiresApi;

import com.facebook.litho.Border;
import com.facebook.litho.ClickEvent;
import com.facebook.litho.Column;
import com.facebook.litho.Component;
import com.facebook.litho.ComponentContext;
import com.facebook.litho.Handle;
import com.facebook.litho.annotations.LayoutSpec;
import com.facebook.litho.annotations.OnCreateLayout;
import com.facebook.litho.annotations.OnEvent;
import com.facebook.litho.annotations.Param;
import com.facebook.litho.annotations.Prop;
import com.facebook.litho.widget.Text;
import com.facebook.litho.widget.TextInput;
import com.facebook.yoga.YogaEdge;
import com.hotfoot.rapid.au.msme.app.pojo.caf.Assessment;
import com.hotfoot.rapid.aubank.sbl.R;

import java.util.ArrayList;
import java.util.List;

@LayoutSpec
public class CAFAssessmentListItem {

  @RequiresApi(api = Build.VERSION_CODES.P)
  @OnCreateLayout
  static Component onCreateLayout(
      ComponentContext c,
      CAFAssessmentFragment cafFragment,
      Assessment assessment,
      @Prop String n) {
    List<String> districtNames = new ArrayList();
    districtNames.add("Chennai");
    districtNames.add("Erode");
    districtNames.add("Coimbatore");
    districtNames.add("Tirunelveli");

    final Handle textInputHandle = new Handle();

    return Column.create(c)
        .child(
            Text.create(c)
                .text(cafFragment.getActivity().getResources().getString(R.string.ro_assessment))
                .marginDip(YogaEdge.ALL, 10.0f)
                .textAlignment(Layout.Alignment.ALIGN_LEFT)
                .textSizeSp(18.0f)
                .textStyle(1)
                .textColor(
                    cafFragment.getActivity().getResources().getColor(R.color.input_text_color)))
        .child(
            Text.create(c)
                .text("CUSTOMER NAME")
                .marginDip(YogaEdge.ALL, 10.0f)
                .textAlignment(Layout.Alignment.ALIGN_NORMAL)
                .textSizeSp(12.0f)
                .textColor(cafFragment.getActivity().getResources().getColor(R.color.text_color)))
        .child(
            TextInput.create(c)
                .hint("")
                .marginDip(YogaEdge.ALL, 10.0f)
                .inputType(InputType.TYPE_CLASS_TEXT)
                .background(
                    cafFragment.getActivity().getResources().getDrawable(R.drawable.ic_apply_line))
                .multiline(false)
                .maxLines(1))
        .child(
            Text.create(c)
                .text("CUSTOMER ADDRESS")
                .paddingDip(YogaEdge.ALL, 1.0f)
                .marginDip(YogaEdge.ALL, 10.0f)
                .textAlignment(Layout.Alignment.ALIGN_NORMAL)
                .textSizeSp(12.0f)
                .textColor(cafFragment.getActivity().getResources().getColor(R.color.text_color)))
        .child(
            TextInput.create(c)
                .hint("")
                .marginDip(YogaEdge.ALL, 10.0f)
                .inputType(InputType.TYPE_CLASS_TEXT)
                .multiline(true)
                .maxLines(5)
                .background(
                    cafFragment.getActivity().getResources().getDrawable(R.drawable.bg_edt_popup)))
        .child(
            Text.create(c)
                .text("CUSTOMER CONTACT NO")
                .marginDip(YogaEdge.ALL, 10.0f)
                .textAlignment(Layout.Alignment.ALIGN_NORMAL)
                .textSizeSp(12.0f)
                .textColor(cafFragment.getActivity().getResources().getColor(R.color.text_color)))
        .child(
            TextInput.create(c)
                .hint("")
                .marginDip(YogaEdge.ALL, 10.0f)
                .inputType(InputType.TYPE_CLASS_PHONE)
                .background(
                    cafFragment.getActivity().getResources().getDrawable(R.drawable.ic_apply_line))
                .multiline(false)
                .maxLines(1))
        .child(
            Text.create(c)
                .text("CUSTOMER BUSINESS")
                .marginDip(YogaEdge.ALL, 10.0f)
                .textAlignment(Layout.Alignment.ALIGN_NORMAL)
                .textSizeSp(12.0f)
                .textColor(cafFragment.getActivity().getResources().getColor(R.color.text_color)))
        .child(
            TextInput.create(c)
                .hint("")
                .marginDip(YogaEdge.ALL, 10.0f)
                .inputType(InputType.TYPE_CLASS_TEXT)
                .background(
                    cafFragment.getActivity().getResources().getDrawable(R.drawable.ic_apply_line))
                .multiline(false)
                .maxLines(1))
        .child(
            Text.create(c)
                .text("LOAN AMOUNT APPLIED")
                .marginDip(YogaEdge.ALL, 10.0f)
                .textAlignment(Layout.Alignment.ALIGN_NORMAL)
                .textSizeSp(12.0f)
                .textColor(cafFragment.getActivity().getResources().getColor(R.color.text_color)))
        .child(
            TextInput.create(c)
                .hint("")
                .marginDip(YogaEdge.ALL, 10.0f)
                .inputType(InputType.TYPE_CLASS_NUMBER)
                .background(
                    cafFragment.getActivity().getResources().getDrawable(R.drawable.ic_apply_line))
                .multiline(false)
                .maxLines(1))
        .child(
            Text.create(c)
                .text("TENURE")
                .marginDip(YogaEdge.ALL, 10.0f)
                .textAlignment(Layout.Alignment.ALIGN_NORMAL)
                .textSizeSp(12.0f)
                .textColor(cafFragment.getActivity().getResources().getColor(R.color.text_color)))
        .child(
            TextInput.create(c)
                .hint("")
                .marginDip(YogaEdge.ALL, 10.0f)
                .inputType(InputType.TYPE_CLASS_NUMBER)
                .background(
                    cafFragment.getActivity().getResources().getDrawable(R.drawable.ic_apply_line))
                .multiline(false)
                .maxLines(1))
        .child(
            Text.create(c)
                .text("EMi")
                .marginDip(YogaEdge.ALL, 10.0f)
                .textAlignment(Layout.Alignment.ALIGN_NORMAL)
                .textSizeSp(12.0f)
                .textColor(cafFragment.getActivity().getResources().getColor(R.color.text_color)))
        .child(
            TextInput.create(c)
                .hint("")
                .marginDip(YogaEdge.ALL, 10.0f)
                .inputType(InputType.TYPE_CLASS_NUMBER)
                .background(
                    cafFragment.getActivity().getResources().getDrawable(R.drawable.ic_apply_line))
                .multiline(false)
                .maxLines(1))
        .child(
            Text.create(c)
                .text("PD WITH")
                .marginDip(YogaEdge.ALL, 10.0f)
                .textAlignment(Layout.Alignment.ALIGN_NORMAL)
                .textSizeSp(12.0f)
                .textColor(cafFragment.getActivity().getResources().getColor(R.color.text_color)))
        .child(
            com.facebook.litho.widget.Spinner.create(c)
                .marginDip(YogaEdge.ALL, 10.0f)
                .options(districtNames)
                .selectedOption(districtNames.get(1))
                .border(
                    Border.create(c)
                        .widthDip(YogaEdge.ALL, 1)
                        .color(YogaEdge.ALL, Color.BLACK)
                        .build()))
        .child(
            Text.create(c)
                .text("PLACE OF PD-ADDRESS")
                .marginDip(YogaEdge.ALL, 10.0f)
                .textAlignment(Layout.Alignment.ALIGN_NORMAL)
                .textSizeSp(12.0f)
                .textColor(cafFragment.getActivity().getResources().getColor(R.color.text_color)))
        .child(
            com.facebook.litho.widget.Spinner.create(c)
                .marginDip(YogaEdge.ALL, 10.0f)
                .options(districtNames)
                .selectedOption(districtNames.get(1))
                .border(
                    Border.create(c)
                        .widthDip(YogaEdge.ALL, 1)
                        .color(YogaEdge.ALL, Color.BLACK)
                        .build()))
        .child(
            Text.create(c)
                .text("PURPOSE OF LOAN")
                .marginDip(YogaEdge.ALL, 10.0f)
                .textAlignment(Layout.Alignment.ALIGN_NORMAL)
                .textSizeSp(12.0f)
                .textColor(cafFragment.getActivity().getResources().getColor(R.color.text_color)))
        .child(
            com.facebook.litho.widget.Spinner.create(c)
                .marginDip(YogaEdge.ALL, 10.0f)
                .options(districtNames)
                .selectedOption(districtNames.get(1))
                .border(
                    Border.create(c)
                        .widthDip(YogaEdge.ALL, 1)
                        .color(YogaEdge.ALL, Color.BLACK)
                        .build()))
        .child(
            Text.create(c)
                .text("SAVE")
                .textSizeSp(14.0f)
                .paddingDip(YogaEdge.ALL, 12.0f)
                .textAlignment(Layout.Alignment.ALIGN_CENTER)
                .marginDip(YogaEdge.ALL, 10.0f)
                .backgroundColor(cafFragment.getActivity().getResources().getColor(R.color.orange))
                .textColor(cafFragment.getActivity().getResources().getColor(R.color.white))
                .border(
                    Border.create(c)
                        .widthDip(YogaEdge.ALL, 1)
                        .color(YogaEdge.ALL, Color.parseColor("#F36F21"))
                        .build()))
        .child(
            Text.create(c)
                .text("NEXT")
                .textSizeSp(14.0f)
                .paddingDip(YogaEdge.ALL, 12.0f)
                .textAlignment(Layout.Alignment.ALIGN_CENTER)
                .marginDip(YogaEdge.ALL, 10.0f)
                .backgroundColor(cafFragment.getActivity().getResources().getColor(R.color.purple))
                .textColor(cafFragment.getActivity().getResources().getColor(R.color.white))
                .border(
                    Border.create(c)
                        .widthDip(YogaEdge.ALL, 1)
                        .color(YogaEdge.ALL, Color.parseColor("#FFA500"))
                        .build()))
        .clickHandler(CAFAssessmentListItem.onEditTextClick(c, textInputHandle))
        .build();
  }

  @OnEvent(ClickEvent.class)
  static void onEditTextClick(ComponentContext c, @Param Handle textInputHandle) {
    String customer = TextInput.getText(c, textInputHandle).toString();
  }
}
4

1 回答 1

0

首先,您的 LayoutSpec 类的名称应该以结尾,Spec并且您不应该直接调用它的方法。当您的 Spec 类将被命名MyComponentSpec时,编译器将生成一个名为的真正 Litho 组件实现MyComponent(注意没有后缀Spec)。以及您应该使用的此类的方法。即,在您的示例中,您引用CAFAssessmentListItem.onEditTextClick(c, textInputHandle)which isSpec的方法(您手动编写的),但您应该使用 Component 的方法(将由编译器生成)。

其次,该组件的所有输入都应标记为@Prop,即@Prop Assessment assessment。同样,因为您不应该onCreateLayout自己使用方法——您将使用生成的 ComponentBuilder。喜欢:

CAFAssessmentListItem.create(c)
  .assessment(myAssessment)
  .n(myString)
  .build()

第三,您不需要片段来检索资源。对此有具体的方法,即

Text.create(c)
  .testRes(resId)
  .textColorRes(colorResId)
  .backgroundColorRes(colorResId2)

第四,您从未绑定textInputHandleTextInput部件以在onClick. 查看此文档:https ://fblitho.com/docs/trigger-events 。您需要添加.handle(textInputHandle)TextInput.

最后,当您已经拥有该颜色的值时,您不需要从字符串中解析.color(Color.parseColor("#F36F21"))颜色 - 您只需使用十六进制 int 即可.color(0xFFF36F21)

把它们加起来:

  1. 重命名您的类,添加Spec到末尾(但不要重命名CAFAssessmentListItem.onEditTextClick调用)。
  2. 添加@PropAssessment参数并从中删除片段参数onCreateLayout
  3. cafFragment.getActivity().getResources().调用替换为仅使用资源 ID 和小部件中的相应方法
  4. 添加.handle(textInputHandle)TextInput小部件
  5. 清理设置颜色值
于 2020-04-03T10:42:30.010 回答