当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现IP地址输入框的方法示例代码

Android实现IP地址输入框的方法示例代码

2019年07月24日  | 移动技术网IT编程  | 我要评论

vlookup函数视频教程,退不回,猎魔卡片

前言

本文主要给大家介绍了关于android实现ip地址格式输入框的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

实现效果图:

解决方案:

      1.添加4个edittext和三个textview

      2.设置textview内容为点,且靠下方。设置edittext背景和边框为透明

      3.为每个edittext添加监听事件

示例代码

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"    //最多三个
  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;

     //循环添加监听事件
 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");
 }
 }
 }


 }


}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网