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

所属分类: 软件编程 / Android 阅读数: 29
收藏 0 赞 0 分享

前言

本文主要给大家介绍了关于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");
 }
 }
 }


 }


}

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

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多