大伯 發表於 2019-8-26 10:39:00

Android开发之输入框EditText介绍

<p>这篇文章主要为大家详细介绍了Android布局之输入框EditText设计,具有一定的参考价值,感兴趣的小伙伴们可以参考一下</p>
<p>现在先简单介绍一下技术点:<br>
1.如何使用圆角输入框和按钮背景<br>
2.如何实现“手机号”、“密码”后面的竖线<br>
3.如何嵌套输入框的布局<br>
4.如何监听输入框的输入事件及删除按钮的动态显示隐藏<br>
1.如何使用圆角输入框和按钮背景<br>
安卓为开发者准备了shape这个xml标签,用于自定义一些形状。<br>
那么我就来定义一个白色的输入框背景。代码如下:</p>
<pre><code class="language-xml">&lt;!-- 形状 --&gt;
&lt;shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" &gt;

&lt;solid android:color="#ffffff" /&gt;
&lt;!-- 边框 --&gt;
&lt;stroke
android:width="1dip"
android:color="#ffffff" /&gt;
&lt;!-- 内填充颜色 --&gt;
&lt;padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" /&gt;
&lt;!-- 圆角 --&gt;
&lt;corners android:radius="6dp" /&gt;
&lt;/shape&gt;
</code></pre>
<pre><code>将其设置成任何View的background就可以了
android:background="@drawable/shape_wihte_frame"
</code></pre>
<p>2.如何实现“手机号”、“密码”后面的竖线<br>
这个其实很简单,只需书写一个竖线即可,宽度为1dp或者1px(或你认为更合适的数值)。</p>
<pre><code class="language-xml">&lt;View
    android:id="@+id/view1"
    android:layout_width="1dip"
    android:layout_height="fill_parent"
    android:layout_centerVertical="true"
    android:layout_gravity="center_horizontal"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_toRightOf="@+id/textView1"
    android:background="#EEEFFF" /&gt;
</code></pre>
<p>3.如何嵌套输入框的布局<br>
安卓给我们提供了多种布局,但是你用任何一种都没办法把界面设计好。必须嵌套,很多新手不敢去嵌套,大家一定要大胆的去嵌套去使用各种布局,一定会组合出炫酷的效果的。这里布局很简单仅仅是一层嵌套(整个页面布局嵌套输入框的布局)。</p>
<pre><code class="language-xml">&lt;RelativeLayout
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_alignParentTop="true"
   android:layout_centerHorizontal="true"
   android:background="@drawable/shape_wihte_frame" &gt;

   &lt;TextView
    android:id="@+id/textView1"
    android:layout_width="40dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:lines="1"
    android:padding="1dp"
    android:text="手机号"
    android:textSize="11sp" /&gt;

   &lt;View
    android:id="@+id/view1"
    android:layout_width="1dip"
    android:layout_height="fill_parent"
    android:layout_centerVertical="true"
    android:layout_gravity="center_horizontal"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_toRightOf="@+id/textView1"
    android:background="#EEEFFF" /&gt;

   &lt;EditText
    android:id="@+id/phonenumber"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_centerVertical="true"
    android:layout_marginLeft="2dp"
    android:layout_toRightOf="@+id/view1"
    android:background="@drawable/transparent"
    android:ems="19"
    android:hint="请输入手机号"
    android:inputType="phone"
    android:padding="1dp"
    android:textSize="12sp" &gt;

    &lt;requestFocus /&gt;
   &lt;/EditText&gt;

   &lt;ImageView
    android:id="@+id/del_phonenumber"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="3dp"
    android:src="@drawable/text_del"
    android:visibility="invisible" /&gt;
&lt;/RelativeLayout&gt;
</code></pre>
<p>4.如何监听输入框的输入事件及删除按钮的动态显示隐藏<br>
思想很简单,就是监听EditText的输入事件,之后如果输入长度大于0就显示后面的删除按钮,如果=0就隐藏删除按键,点击删除按钮就清空输入框。在这里我写出了一个工具类方便大家调用。高内聚低耦合是我们共同的追求。</p>
<pre><code class="language-java">public class EditTextClearTools {
public static void addclerListener(final EditText e1, final ImageView m1) {

e1.addTextChangedListener(new TextWatcher() {

   @Override
   public void onTextChanged(CharSequence s, int start, int before,
   int count) {
    // TODO Auto-generated method stub

   }

   @Override
   public void beforeTextChanged(CharSequence s, int start, int count,
   int after) {
    // TODO
   }

   @Override
   public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
    // 监听如果输入串长度大于0那么就显示clear按钮。
    String s1 = s + "";
    if (s.length() &gt; 0) {
   m1.setVisibility(View.VISIBLE);
    } else {
   m1.setVisibility(View.INVISIBLE);
    }

   }
});

m1.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    // 清空输入框
    e1.setText("");

   }
});

}

}
</code></pre>
<p>主程序代码</p>
<pre><code class="language-java">public class MainActivity extends Activity {
EditText e1, e2;
ImageView m1, m2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_user_login);
init();
}

private void init() {
// TODO Auto-generated method stub
e1 = (EditText) findViewById(R.id.phonenumber);
e2 = (EditText) findViewById(R.id.password);
m1 = (ImageView) findViewById(R.id.del_phonenumber);
findViewById(R.id.del_phonenumber);
m2 = (ImageView) findViewById(R.id.del_password);
// 添加清楚监听器大气
EditTextClearTools.addclerListener(e1, m1);
EditTextClearTools.addclerListener(e2, m2);

}
}
</code></pre>
<p>输入框的应用当下来说也十分广泛,在这里仅仅是简单介绍一下输入框控件,后面会陆续更新一些比较好的技术和个人见解,感谢大家支持!</p><br><br>
来源:https://www.cnblogs.com/blizzawang/p/11411172.html
頁: [1]
查看完整版本: Android开发之输入框EditText介绍