前言
“專業(yè)、務(wù)實(shí)、高效、創(chuàng)新、把客戶的事當(dāng)成自己的事”是我們每一個(gè)人一直以來(lái)堅(jiān)持追求的企業(yè)文化。 創(chuàng)新互聯(lián)建站是您可以信賴的網(wǎng)站建設(shè)服務(wù)商、專業(yè)的互聯(lián)網(wǎng)服務(wù)提供商! 專注于網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、軟件開(kāi)發(fā)、設(shè)計(jì)服務(wù)業(yè)務(wù)。我們始終堅(jiān)持以客戶需求為導(dǎo)向,結(jié)合用戶體驗(yàn)與視覺(jué)傳達(dá),提供有針對(duì)性的項(xiàng)目解決方案,提供專業(yè)性的建議,創(chuàng)新互聯(lián)建站將不斷地超越自我,追逐市場(chǎng),引領(lǐng)市場(chǎng)!
本文主要給大家介紹了關(guān)于Android實(shí)現(xiàn)IP地址格式輸入框的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
實(shí)現(xiàn)效果圖:
解決方案:
1.添加4個(gè)EditText和三個(gè)TextView
2.設(shè)置TextView內(nèi)容為點(diǎn),且靠下方。設(shè)置EditText背景和邊框?yàn)橥该?/p>
3.為每個(gè)EditText添加監(jiān)聽(tīng)事件
示例代碼
Layout:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="6dp" android:layout_weight="4" android:background="@drawable/ip_input_shape"> <EditText android:id="@+id/IP_1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@null" android:gravity="center_horizontal" android:inputType="number" //輸入類型 android:lines="1" android:maxLength="3" //最多三個(gè) android:textSize="24sp" android:imeOptions="actionNext"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="bottom" android:text="." /> <EditText android:id="@+id/IP_2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@null" android:gravity="center_horizontal" android:inputType="number" android:lines="1" android:maxLength="3" android:textSize="24sp" android:imeOptions="actionNext"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="." /> <EditText android:id="@+id/IP_3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@null" android:gravity="center_horizontal" android:inputType="number" android:lines="1" android:maxLength="3" android:textSize="24sp" android:imeOptions="actionNext"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="." /> <EditText android:id="@+id/IP_4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@null" android:gravity="center_horizontal" android:inputType="number" android:lines="1" android:maxLength="3" android:textSize="24sp" android:imeOptions="actionNext"/> </LinearLayout> <Button android:id="@+id/Save_Ip" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="Save" /> </LinearLayout>
Java:
public class SystemConfig extends AppCompatActivity implements View.OnClickListener { private DrawerLayout configbar; private SharedPreferences.Editor editor; private EditText ip_1; private EditText ip_2; private EditText ip_3; private EditText ip_4; private Button save_ip_btn; String[] IP_List = null; @Override public void onClick(View v) { switch (v.getId()) { case R.id.Save_Ip: if (ip_1.getText().length() == 0 || ip_2.getText().length() == 0 || ip_3.getText().length() == 0 || ip_4.getText().length() == 0) { Toast.makeText(this, "IP地址不正確!", Toast.LENGTH_SHORT).show(); break; } String IP_result = ip_1.getText() + "." + ip_2.getText() + "." + ip_3.getText() + "." + ip_4.getText(); editor.putString("DB_IP", IP_result); editor.apply(); Toast.makeText(this, "保存成功!", Toast.LENGTH_SHORT).show(); break; default: break; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.system_config); SharedPreferences preferences = getSharedPreferences("System_Config", MODE_PRIVATE); editor = preferences.edit(); ip_1 = (EditText) findViewById(R.id.IP_1); ip_2 = (EditText) findViewById(R.id.IP_2); ip_3 = (EditText) findViewById(R.id.IP_3); ip_4 = (EditText) findViewById(R.id.IP_4); save_ip_btn = (Button) findViewById(R.id.Save_Ip); save_ip_btn.setOnClickListener(this); TextChangeListen[] mTextWatcher = new TextChangeListen[4]; EditText[] editTexts_List = new EditText[4]; editTexts_List[0] = ip_1; editTexts_List[1] = ip_2; editTexts_List[2] = ip_3; editTexts_List[3] = ip_4; //循環(huán)添加監(jiān)聽(tīng)事件 for (int i = 0; i < 4; i++) { mTextWatcher[i] = new TextChangeListen(editTexts_List[i]); editTexts_List[i].addTextChangedListener(mTextWatcher[i]); } boolean zhaji = preferences.getBoolean("IsZhaJi", false); String Data_IP = preferences.getString("DB_IP", "192.168.0.118"); IP_List = Data_IP.split("\\."); ip_1.setText(IP_List[0]); ip_2.setText(IP_List[1]); ip_3.setText(IP_List[2]); ip_4.setText(IP_List[3]); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); break; default: break; } return true; } public class TextChangeListen implements TextWatcher { public EditText IP_Edit; public TextChangeListen(EditText IP_Edit) { super(); this.IP_Edit = IP_Edit; } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { if (s.length() == 3) { if (Integer.parseInt(s.toString()) <= 255) { if (this.IP_Edit == ip_1) { ip_2.requestFocus(); } if (this.IP_Edit == ip_2) { ip_3.requestFocus(); } if (this.IP_Edit == ip_3) { ip_4.requestFocus(); } } else { Toast.makeText(SystemConfig.this, "IP格式不正確!", Toast.LENGTH_SHORT).show(); this.IP_Edit.setText("0"); } } else if (s.length() == 0) { if (this.IP_Edit == ip_1) { ip_1.setText("0"); } if (this.IP_Edit == ip_2) { ip_1.requestFocus(); ip_2.setText("0"); } if (this.IP_Edit == ip_3) { ip_2.requestFocus(); ip_3.setText("0"); } if (this.IP_Edit == ip_4) { ip_3.requestFocus(); ip_4.setText("0"); } } } } }
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持。
文章標(biāo)題:Android實(shí)現(xiàn)IP地址輸入框的方法示例代碼
文章來(lái)源:http://chinadenli.net/article26/jgjecg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、電子商務(wù)、品牌網(wǎng)站設(shè)計(jì)、面包屑導(dǎo)航、域名注冊(cè)、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)