您好,登录后才能下订单哦!
本篇文章给大家分享的是有关EditText怎么在Android中使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。
android:background="@null" 或android:background="@/drawable/bg_edittext_norma.xml"
bg_edittext_norma.xml
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!--商品描述的可编辑框--> <solid android:color="#FFFFFF" /> <corners android:radius="10dip"/> <stroke android:width="1dip" android:color="#BDC7D8" /> </shape>
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" android:hint="输入用户名" android:paddingBottom="5dip" android:paddingTop="5dip" />


核心代码块1:
这俩个一定要设置,要不然软键盘不会出现搜索
android:imeOptions="actionSearch" android:singleLine="true"
核心代码块2:
Activity或者Fragment 要实现TextView.OnEditorActionListener接口
public class DrugCatalogueInquiryFragment extends GeneralSocialFragment implements TextView.OnEditorActionListener {
 private ClearEditText etDrugName;
 etDrugName = xFindViewById(R.id.et_drug_name);
 etDrugName.setOnEditorActionListener(this);
  @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        doWhichOperation(actionId);
        return true;
    }
    private void doWhichOperation(int actionId) {
        switch (actionId) {
            case EditorInfo.IME_ACTION_SEARCH:
                //隐藏项目中弹框
                hideSoftInputMethod();
                //项目中个性化操作
                getEditTextValue();
                pageno = 1;
                getMedicineListInfoForApp(name,firstWord,type,level,pageno);
                break;
            default:
                break;
        }
    }
}关键代码
android:gravity="left"
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:minLines="5" android:background="#ffffff" android:paddingLeft="5dp" android:gravity="left" />
像这种。这是什么原因造成的呢?用来EdittText默认是gravity是center.就是从中间对齐。我们把他改成left啊top啊就OK了。
在使用EditText的XML 文件中加入一个属性:
android:textCursorDrawable="@null" //或者 android:textCursorDrawable = "#fff000"
这个属性是用来控制光标颜色的,"@null" 是作用是让光标颜色和text color一样,当然也可以修改成你自己的颜色。
private void initListener(){
        etDrugName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){
                    TypeUtils.getInstance( getActivity() ).hideKeyboardView();
                }
            }
        });
        etDrugNameOfInitial.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){
                    TypeUtils.getInstance( getActivity() ).hideKeyboardView();
                }
            }
        });
    }android:ellipsize="end"

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  /**
     * 通过使用Android源码中的InputFilter接口
     */
    InputFilter filter = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {
            for (int i = start; i < end; i++) {
                if (!isChinese(source.charAt(i))) {
                    return "";
                }
            }
            return null;
        }
    };
    /**
     * 判定输入汉字
     *
     * @param c
     * @return
     */
    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }以上就是EditText怎么在Android中使用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。