Java 如何在android中增加字母数字?

Java 如何在android中增加字母数字?,java,auto-increment,alphanumeric,Java,Auto Increment,Alphanumeric,我不熟悉android编码实践 ABB20180001 假设这是我的第一个id,我希望使用共享首选项将其自动递增1,并将其用作员工id。 如ABB20180002、ABB20180003、ABB20180004等。您可以使用特定的基数将数字解析为一个长的递增数字,然后将其转换回字符串 如果使用的是所有字母,则可以使用36作为基数: long number = Long.parseLong("ABB20180001", 36); String incremented = Long.toStrin

我不熟悉android编码实践

ABB20180001
假设这是我的第一个id,我希望使用共享首选项将其自动递增1,并将其用作员工id。
如ABB20180002、ABB20180003、ABB20180004等。

您可以使用特定的
基数
将数字解析为一个长的递增数字,然后将其转换回字符串

如果使用的是所有字母,则可以使用
36
作为基数:

long number = Long.parseLong("ABB20180001", 36);

String incremented = Long.toString(number + 1, 36).toUpperCase();//"ABB20180002"
你的数字可能只是一个十六进制数。在这种情况下,您可以使用
16
作为基数,而不是如上所示的
36

请注意,如果ABB只是一个前缀,则上述操作将不起作用(增加20将返回
ABB2018000L

如果
“ABB”
只是一个静态前缀,那么您可以使用

//if the prefix changes, a regex will be needed
String incremented = "ABB" + (Long.parseLong(string.replace("ABB", "")) + 1)
最后,如果
“ABB”
可以更改,您可以使用这样的正则表达式(下面的示例假设前缀的长度为3,相应地更改):

String s=“ABB20180001”;

String[]parts=s.split((?您不能直接递增字母数字值。如果要这样做,您需要为它编写一些代码行

这里是activity\u main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txt_autoincreament"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="click"
        android:onClick="Click"/>

</LinearLayout>

希望这能对你有所帮助。

ABB20180001
不是有效的
int
我的朋友你不能递增它是一个十六进制数吗?你是要使用所有字符,还是只使用
ABCDEF
?它只是字母数字,我需要作为ABB20180002,ABB20180003等。你的答案是递增值,并用一些预定义的字符串连接它。你添加Android代码的原因是什么?我通过添加几行代码可以帮助他快速理解。因为他是Android开发的新手。几天前我检查了
parseInteger
的工作原理,但在阅读这个问题时,没有发现它可以在这里使用。我喜欢第一种方法!这应该可以接受。+1基数法。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txt_autoincreament"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="click"
        android:onClick="Click"/>

</LinearLayout>
public class MainActivity extends AppCompatActivity {
    TextView autoTextIncreament;
    String stringValue="ABB";
    long intValue=20180001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        autoTextIncreament = findViewById(R.id.txt_autoincreament);
    }

    public void Click(View view){
        autoTextIncreament.setText(getValue());
    }

    private String getValue() {
        return stringValue+String.valueOf(intValue++);
    }
}