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

 更新时间:2017年10月11日 10:36:41   作者:XinYiBuFang  
输入框是我们日常开发中经常遇到的一个控件,如果更好的控制输入框是对用户体验很重要的一步,所以下面这篇文章主要给大家介绍了关于Android如何实现IP输入框的相关资料,需要的朋友可以参考下。

前言

本文主要给大家介绍了关于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 使用okhttp可能引发OOM的一个点

    android 使用okhttp可能引发OOM的一个点

    这篇文章主要介绍了android 使用okhttp可能引发OOM的一个点,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • Android AS为xutils添加依赖过程图解

    Android AS为xutils添加依赖过程图解

    这篇文章主要介绍了Android AS为xutils添加依赖过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • android 加载本地联系人实现方法

    android 加载本地联系人实现方法

    在android开发过程中,有些功能需要访问本地联系人列表,本人搜集整理了一番,拿出来和大家分享一下,希望可以帮助你们
    2012-12-12
  • Android 第三方应用接入微信平台研究情况分享(一)

    Android 第三方应用接入微信平台研究情况分享(一)

    微信平台开放后倒是挺火的,许多第三方应用都想试下接入微信这个平台,毕竟可以利用微信建立起来的关系链来拓展自己的应用还是挺不错的 最近由于实习需要也在研究这个东西,这里把我的整个研究情况给出来
    2013-01-01
  • Android空心圆及层叠效果实现代码

    Android空心圆及层叠效果实现代码

    这篇文章主要为大家详细介绍了Android空心圆及层叠效果实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Android应用开发中使用Fragment的入门学习教程

    Android应用开发中使用Fragment的入门学习教程

    这篇文章主要介绍了Android应用开发中Fragment的入门学习教程,可以把Fragment看作为Activity基础之上的模块,需要的朋友可以参考下
    2016-02-02
  • Android自定义view实现圆形waveview

    Android自定义view实现圆形waveview

    这篇文章主要为大家详细介绍了Android自定义view实现圆形waveview,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • Flutter 首页必用组件NestedScrollView的示例详解

    Flutter 首页必用组件NestedScrollView的示例详解

    今天介绍的组件是NestedScrollView,大部分的App首页都会用到这个组件。对Flutter 首页必用组件NestedScrollView的相关知识感兴趣的一起看看吧
    2020-05-05
  • Android自动测试工具Monkey

    Android自动测试工具Monkey

    Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中。它向系统发送伪随机的用户事件流(如按键输入、触摸屏输入、手势输入等),实现对正在开发的应用程序进行压力测试。Monkey测试是一种为了测试软件的稳定性、健壮性的快速有效的方法
    2016-01-01
  • Android实现Service下载文件,Notification显示下载进度的示例

    Android实现Service下载文件,Notification显示下载进度的示例

    本篇文章主要介绍了Android实现Service下载文件,Notification显示下载进度,具有一定的参考价值,有兴趣的可以了解一下。
    2017-01-01

最新评论