简体   繁体   English

如何在不使用 toast 消息的情况下在微调器和按钮上设置错误消息,例如在 edittext 上?

[英]how do I set an Error message on spinner and button like on edittext without using toast message?

I am trying to set an error message on spinner when items are not selected.当未选择项目时,我试图在微调器上设置错误消息。 Also I would love focusing to spinner when the error occurs.我也很想在发生错误时专注于微调器。 I tried some codes for it but it doesn't work well.我为此尝试了一些代码,但效果不佳。 Especially, the code for focusing on spinner.特别是,专注于微调器的代码。

You can see my codes below.你可以在下面看到我的代码。

public boolean controlCheck(){

    int selectedItemOfMySpinner=spinner.getSelectedItemPosition();
   // String actualPositionOfMySpinner= (String) spinner.getItemAtPosition(selectedItemOfMySpinner);

    if(selectedItemOfMySpinner<1){

        Toast.makeText(context, "LOKANATA:)", Toast.LENGTH_SHORT).show();

        TextView errorText= (TextView) spinner.getSelectedView();
        errorText.setError("");
        errorText.setTextColor(Color.RED);
        errorText.setText("my actual error text");

        return false;
    }

    if(TextUtils.isEmpty(btnDate.getText().toString())){

        btnDate.setError("Bu alan boş olamaz");
        btnDate.requestFocus();

        return false;
    }

    if(TextUtils.isEmpty(btnTime.getText().toString())){

        btnTime.setError("Bu alan boş olamaz");
        btnTime.requestFocus();
        return false;
    }

    if(TextUtils.isEmpty(kisisayisi.getText().toString())){

        kisisayisi.setError("Bu alan boş olamaz");
        kisisayisi.requestFocus();

        return false;
    }

    if(TextUtils.isEmpty(edtTel.getText().toString())){

        edtTel.setError("Bu alan boş olamaz");
        edtTel.requestFocus();
        return false;
    }

    if(edtTel.getText().toString().length()<18)
    {
        edtTel.setError("telefon numarasını eksiksiz giriniz");
        edtTel.requestFocus();

        return false;
    }

    if(TextUtils.isEmpty(namesurname.getText().toString())){

        namesurname.setError("Bu alan boş olamaz");
        namesurname.requestFocus();

        return false;
    }

    return true;
}

Any suggestion?有什么建议吗?

Use this用这个

Spinner in layout布局中的微调器

<Spinner
    android:id="@+id/mySpinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/btn_dropdown"
/>

Adding adapter to spinner in activity在活动中将适配器添加到微调器

final Spinner mySpinner = (Spinner) findViewById(R.id.mySpinner);

ArrayList<String> list = new ArrayList<>();
list.add("Select");
list.add("Enamul");
list.add("Tonu");

ArrayAdapter spinnerAdapter =  new ArrayAdapter(getApplicationContext(),
                   R.layout.cutom_spinner_layout,  list);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(spinnerAdapter);

cutom_spinner_layout.xml that use layout resource in adapter在适配器中使用布局资源的 cutom_spinner_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:gravity="left|center_vertical|center_horizontal"
android:textColor="#000"
android:padding="2dp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:fadingEdge="horizontal"
android:scrollHorizontally="true"
android:selectAllOnFocus="true"
android:layout_gravity="center_horizontal"
/>

Add error添加错误

if(mySpinner.getSelectedItemPosition()<1){
     TextView errorText= (TextView) mySpinner.getSelectedView();
     errorText.setError("");
     errorText.setTextColor(Color.RED);
     errorText.setText("Please select one option");
}

Output when error出错时输出

在此处输入图片说明

Add these codes inside the textview in xml.在 xml 中的 textview 中添加这些代码。

<TextView 
        ...       
        android:focusable="true"
        android:focusableInTouchMode="true" 
/>

Add this line below set text where you are showing error for spinner在显示微调器错误的设置文本下方添加此行

 errorText.requestFocus();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM