【问题标题】:GetText() for a chipgroup (entry)GetText() 用于芯片组(条目)
【发布时间】:2020-04-30 18:45:45
【问题描述】:

我是 java/android 的新手,我需要一个项目来实现芯片(入口)。我按照教程进行操作,芯片工作正常。 what i made

XML

<Button
    android:id="@+id/email_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="buttonClick"
    android:text="Ajouter"
    android:textSize="10sp"
    app:layout_constraintBottom_toTopOf="@+id/chip_group"
    app:layout_constraintLeft_toRightOf="@+id/fields_2_form"
    app:layout_constraintTop_toBottomOf="@+id/title_field_3">

</Button>
<com.google.android.material.chip.ChipGroup
    android:id="@+id/chip_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    app:layout_constraintTop_toBottomOf="@+id/fields_2_form">

</com.google.android.material.chip.ChipGroup>

活动

  public void buttonClick(View view) {
    final EditText fieldForm2 = findViewById(R.id.fields_2_form);
    final ChipGroup chipGroup = findViewById(R.id.chip_group);
    final Button emailButton = findViewById(R.id.email_button);

    final Chip chip = new Chip(this);
    ChipDrawable drawable = ChipDrawable
    .createFromAttributes(this, null, 0, R.style.Widget_MaterialComponents_Chip_Entry);
    chip.setChipDrawable(drawable);
    chip.setCheckable(false);
    chip.setClickable(false);
    chip.setChipIconResource(R.drawable.ic_fingerprint_black_24dp);
    chip.setIconStartPadding(3f);
    chip.setPadding(60, 10, 60, 10);
    chip.setText(fieldForm2.getText().toString());

    // remove chip on click on the -
    chip.setOnCloseIconClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            chipGroup.removeView(chip);
        }
    });
    if (!EmailValid(fieldForm2.getText().toString())) {
        Toast.makeText(getApplicationContext(),
                "Veuillez rentrer un email valide",
                Toast.LENGTH_LONG).show();
        fieldForm2.setText("");
    } else {

        chipGroup.addView(chip);
        fieldForm2.setText("");
    }
}

但是我的问题是当我需要从芯片中获取所有键入的电子邮件以进行另一项活动时。 我试图用这种方法做到这一点:

public Intent getFormInfos() {

    TextView mEditTextHour = findViewById(R.id.fields_1_form);
    final Spinner spin = (Spinner) findViewById(R.id.Spinner);
    final ChipGroup chipGroup = findViewById(R.id.chip_group);


    //FIXME
    String chiptext = chipGroup.INEEDTOGETCHIPTEXTHERE();


    String myEditedText1 = mEditTextHour.getText().toString();
    String spinnerText = spin.getSelectedItem().toString();


    Intent intent = new Intent(this, ActivityListMareu.class);
    ApiService.getmReunions().add(new Reunion(myEditedText1, spinnerText, chiptext));


    return intent;
}

我需要做一些类似chipgroup.getText()的事情,但它不起作用我尝试了一些东西,但我真的对芯片感到困惑,实际上我不知道如何制作它。 有人对此有想法吗?
(对不起我的英语不好)

【问题讨论】:

    标签: java android android-chips


    【解决方案1】:

    使用它,您可以在该ChipGroup 中获取所有Chip 的所有文本集。

            ArrayList<String> emails = new ArrayList<>();
    
            for (int i = 0; i < chipGroup.getChildCount(); i++) {
                String email = ((Chip) chipGroup.getChildAt(i)).getText().toString();
                emails.add(email);
            }
    

    如果您在chipGroup 中只有一个chip,您可以直接:

            if (chipGroup.getChildCount() > 0) {
                CharSequence yourText = ((Chip) chipGroup.getChildAt(0)).getText();
            }
    

    【讨论】:

      【解决方案2】:

      这是在 Kotlin 中,但您基本上可以对 getChildren() 进行转换,就像任何集合/序列一样:

      val emails = chipGroup.children.map { 
        (it as Chip).text.toString() 
      }.toList()
      

      【讨论】:

        猜你喜欢
        • 2020-08-25
        • 2020-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-16
        • 2018-12-14
        • 2019-05-23
        相关资源
        最近更新 更多