zoukankan      html  css  js  c++  java
  • Android_EditText

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5        android:orientation="horizontal"
     6     tools:context="com.example.edittext.MainActivity" >
     7     <!-- EditText的常用属性
     8         android:hint 当编辑框内容为空时的提示
     9         android:inputType 限制输入的内容 
    10         android:digits 限制输入的字符 
    11         requestFocus 自动获得焦点-->
    12         <EditText 
    13             android:layout_width="wrap_content"
    14             android:layout_height="wrap_content"
    15             android:inputType="textCapCharacters"
    16             android:hint="输入大写字母"
    17            />
    18         <EditText 
    19             android:layout_width="wrap_content"
    20             android:layout_height="wrap_content"
    21             android:inputType="textCapWords"
    22             android:hint="单词首字母为大写"
    23            />
    24         <EditText 
    25             android:layout_width="wrap_content"
    26             android:layout_height="wrap_content"
    27             android:inputType="textCapSentences"
    28             android:hint="句子首字母为大写"
    29            />
    30         <EditText 
    31             android:layout_width="wrap_content"
    32             android:layout_height="wrap_content"
    33             android:digits="0123456789abcdef"
    34             android:hint="限定数字16进制数"
    35            />
    36         <EditText 
    37             android:layout_width="wrap_content"
    38             android:layout_height="wrap_content"
    39             android:inputType="textCapSentences"
    40             android:hint="自动获得焦点">
    41             <requestFocus/>
    42          </EditText>
    43           
    44 </LinearLayout>

    EditText设置光标颜显示:

    需要在布局文件中指定androd:textCursorDrawable,如果需要设置成与字体一样的颜色,改属性设置为“@null”即可,如果需要自定义颜色,需要自定义一个drawable文件,例如:在drawable下窗井my_cursor.xml,内容如下

    复制代码
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <solid android:color="#000080" />
        <size android:width="1dp"/>
    
    </shape>
    复制代码

    然后,设置android:textCursorDrawable="@drawable/my_cursor",光标颜色就可以改变为指定颜色。

    EditText文本设置光标位置:

     EditText文本设置为右对齐,并设置有提示文字时,光标默认是在左侧显示的,这里解决的方法为创建一个TextView代替提示按钮,设置在Edittext下方,并在代码中通过监听Edittext的状态,来控制Textview的显示与隐藏,进而达到提示文字在右侧,光标也在右侧的效果。

    xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    
        xmlns:tools="http://schemas.android.com/tools"    
        android:id="@+id/activity_main"    
        android:layout_width="match_parent"    
        android:layout_height="match_parent"      
        android:orientation="vertical"    
        tools:context="com.eichinn.practice.MainActivity">    
        <FrameLayout        
            android:layout_width="match_parent"        
            android:layout_height="wrap_content">        
        <EditText            
            android:id="@+id/et"            
            android:layout_width="match_parent"            
            android:layout_height="match_parent"            
            android:gravity="right|center_vertical" />        
        <TextView            
            android:id="@+id/tv"            
            android:layout_width="match_parent"            
            android:layout_height="match_parent"            
            android:layout_marginRight="6dp"            
            android:gravity="right|center_vertical"  
            android:background="@null"          
            android:text="Hello World!"/>    
        </FrameLayout>
    </LinearLayout>

    java代码如下:

    public class MainActivity extends AppCompatActivity {    
        private EditText et;    
        private TextView tv;   
        @Override   
        protected void onCreate(Bundle savedInstanceState) {        
            super.onCreate(savedInstanceState);        
            setContentView(R.layout.activity_main);       
            et = (EditText) findViewById(R.id.et);       
            tv = (TextView) findViewById(R.id.tv);       
            et.addTextChangedListener(new TextWatcher() {            
                @Override          
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {           
                }            
                @Override           
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {                
                    if (TextUtils.isEmpty(charSequence)) {                    
                        tv.setVisibility(View.VISIBLE);                
                    } else {                    
                        tv.setVisibility(View.GONE);                
                    }            
                }            
                @Override           
                public void afterTextChanged(Editable editable) {            
                }        
            });    
        }
    }
    

    参考链接:https://www.jianshu.com/p/a7e88cedce1d

  • 相关阅读:
    C#:foreach语句,yield语句
    C#:委托
    C#:事件
    fckeditor 添加上传附件功能
    电话号码 正则表达式
    设为首页,和加入收藏js代码
    sql中判断时间,精确到秒
    js 日期 星期
    那些惊艳的句子!
    .net 动态页面生成静态页面
  • 原文地址:https://www.cnblogs.com/fangg/p/5558307.html
Copyright © 2011-2022 走看看