国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實現IP地址輸入框的方法示例代碼

2019-10-22 18:26:09
字體:
來源:轉載
供稿:網友

前言

本文主要給大家介紹了關于Android實現IP地址格式輸入框的相關內容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。

實現效果圖:

android,ip地址輸入框,輸入框樣式,輸入框

解決方案:

      1.添加4個EditText和三個TextView

      2.設置TextView內容為點,且靠下方。設置EditText背景和邊框為透明

      3.為每個EditText添加監(jiān)聽事件

示例代碼

Layout:

<LinearLayout android/234369.html">android/210522.html">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"    //最多三個  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)聽事件 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"); } } } }}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 西宁市| 贵港市| 大理市| 焦作市| 富锦市| 彭州市| 科技| 平舆县| 常德市| 荥经县| 汉中市| 前郭尔| 英吉沙县| 高要市| 弋阳县| 土默特左旗| 许昌市| 乌拉特中旗| 和林格尔县| 双峰县| 洱源县| 凌海市| 镇远县| 交口县| 平邑县| 张家界市| 柳州市| 仲巴县| 丘北县| 珲春市| 浦江县| 连平县| 航空| 东莞市| 沙田区| 南漳县| 永胜县| 乌苏市| 买车| 三穗县| 博罗县|