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"); } } } } }
原文地址:https://www.cnblogs.com/xinyibufang/p/7463593.html