這篇文章將為大家詳細(xì)講解有關(guān)Android中EditText怎么用,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)是一家專業(yè)提供鶴崗企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站制作、成都做網(wǎng)站、H5頁面制作、小程序制作等業(yè)務(wù)。10年已為鶴崗眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。大家先看一下效果圖
當(dāng)用戶輸入時動態(tài)出現(xiàn)刪除按鈕
現(xiàn)在先羅列一下技術(shù)點(diǎn):
1.如何使用圓角輸入框和按鈕背景
2.如何實(shí)現(xiàn)“手機(jī)號”、“密碼”后面的豎線
3.如何嵌套輸入框的布局
4.如何監(jiān)聽輸入框的輸入事件及刪除按鈕的動態(tài)顯示隱藏
1.如何使用圓角輸入框和按鈕背景
安卓為開發(fā)者準(zhǔn)備了shape這個xml標(biāo)簽,用于自定義一些形狀。
那么我就來定義一個白色的輸入框背景。代碼如下:
<!-- 形狀 --> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="#ffffff" /> <!-- 邊框 --> <stroke android:width="1dip" android:color="#ffffff" /> <!-- 內(nèi)填充顏色 --> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" /> <!-- 圓角 --> <corners android:radius="6dp" /> </shape>
將其設(shè)置成任何View的background就可以了
android:background="@drawable/shape_wihte_frame"
2.如何實(shí)現(xiàn)“手機(jī)號”、“密碼”后面的豎線
這個其實(shí)很簡單,只需書寫一個豎線即可,寬度為1dp或者1px(或你認(rèn)為更合適的數(shù)值)。
<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" />
3.如何嵌套輸入框的布局
安卓給我們提供了多種布局,但是你用任何一種都沒辦法把界面設(shè)計(jì)好。必須嵌套,很多新手不敢去嵌套,大家一定要大膽的去嵌套去使用各種布局,一定會組合出炫酷的效果的。這里布局很簡單僅僅是一層嵌套(整個頁面布局嵌套輸入框的布局)。
<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" > <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="手機(jī)號" android:textSize="11sp" /> <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" /> <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="請輸入手機(jī)號" android:inputType="phone" android:padding="1dp" android:textSize="12sp" > <requestFocus /> </EditText> <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" /> </RelativeLayout>
4.如何監(jiān)聽輸入框的輸入事件及刪除按鈕的動態(tài)顯示隱藏
思想很簡單,就是監(jiān)聽EditText的輸入事件,之后如果輸入長度大于0就顯示后面的刪除按鈕,如果=0就隱藏刪除按鍵,點(diǎn)擊刪除按鈕就清空輸入框。在這里我寫出了一個工具類方便大家調(diào)用。高內(nèi)聚低耦合是我們共同的追求。
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 Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub // 監(jiān)聽如果輸入串長度大于0那么就顯示clear按鈕。 String s1 = s + ""; if (s.length() > 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(""); } }); } }
主程序代碼
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); m2 = (ImageView) findViewById(R.id.del_password); // 添加清楚監(jiān)聽器大氣 EditTextClearTools.addclerListener(e1, m1); EditTextClearTools.addclerListener(e2, m2); } }
關(guān)于“Android中EditText怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
分享標(biāo)題:Android中EditText怎么用-創(chuàng)新互聯(lián)
分享URL:http://aaarwkj.com/article32/ccjcsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、定制網(wǎng)站、搜索引擎優(yōu)化、網(wǎng)站策劃、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容