Android實(shí)現(xiàn)IP地址輸入框的方法示例代碼
前言
本文主要給大家介紹了關(guān)于Android實(shí)現(xiàn)IP地址格式輸入框的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。
實(shí)現(xiàn)效果圖:

解決方案:
1.添加4個EditText和三個TextView
2.設(shè)置TextView內(nèi)容為點(diǎn),且靠下方。設(shè)置EditText背景和邊框?yàn)橥该?/p>
3.為每個EditText添加監(jiān)聽事件
示例代碼
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;
//循環(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");
}
}
}
}
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- Android開發(fā)獲取手機(jī)內(nèi)網(wǎng)IP地址與外網(wǎng)IP地址的詳細(xì)方法與源碼實(shí)例
- Android獲得設(shè)備狀態(tài)信息、Mac地址、IP地址的方法
- Android中使用adb命令通過IP地址連接手機(jī)
- Android開發(fā)實(shí)現(xiàn)在Wifi下獲取本地IP地址的方法
- Android 判斷ip地址合法實(shí)現(xiàn)代碼
- Android 獲取IP地址的實(shí)現(xiàn)方法
- android 獲取本機(jī)的IP地址和mac物理地址的實(shí)現(xiàn)方法
- android實(shí)現(xiàn)獲取有線和無線Ip地址的方法
- Android手機(jī)獲取IP地址的兩種方法
- Android開發(fā)準(zhǔn)確獲取手機(jī)IP地址的兩種方式
相關(guān)文章
Android小程序?qū)崿F(xiàn)選項(xiàng)菜單
這篇文章主要為大家詳細(xì)介紹了Android小程序?qū)崿F(xiàn)選項(xiàng)菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05
Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)方法總結(jié)
這篇文章主要介紹了Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)方法,實(shí)例總結(jié)了屏幕旋轉(zhuǎn)的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04
Android Studio default not found錯誤解決辦法
這篇文章主要介紹了Android Studio gradle 編譯提示‘default not found’ 解決辦法的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android百度地圖應(yīng)用開發(fā)基礎(chǔ)知識
這篇文章主要為大家詳細(xì)介紹了Android百度地圖應(yīng)用開發(fā)基礎(chǔ)知識,為開發(fā)百度地圖應(yīng)用做準(zhǔn)備,感興趣的小伙伴們可以參考一下2016-06-06
Android橫豎屏切換及其對應(yīng)布局加載問題詳解
這篇文章主要為大家詳細(xì)介紹了Android橫豎屏切換及其對應(yīng)布局加載問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
Android實(shí)現(xiàn)點(diǎn)擊縮略圖放大效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)點(diǎn)擊縮略圖放大效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09

