Get integer value from dimens.xml resource file in Android

前端 未结 6 2022
暗喜
暗喜 2020-12-20 13:01

I have an EditText which is decimal and I set its length using android:maxLength property in xml:

    

        
相关标签:
6条回答
  • 2020-12-20 13:20

    With your example, why don't you use this method?

    XML

    <integer name="quantity_length">10</integer>
    

    JAVA

    getResources().getInteger(R.integer.quantity_length);
    
    0 讨论(0)
  • 2020-12-20 13:26

    Why not store the integer in res/integers.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <integer name="quantity_length">12</integer>
    </resources>
    

    And to access the values in code

    int myInteger = getResources().getInteger(R.integer.quantity_length);
    

    Or in XML

    android:maxLength="@integer/quantity_length"
    
    0 讨论(0)
  • 2020-12-20 13:26

    You dont need a TypedValue use the following

    (int) this.getResources().getDimension(R.integer.quantity_length);

    0 讨论(0)
  • 2020-12-20 13:28

    Use:

    Integer.ParseInt(digitsBefore);
    
    0 讨论(0)
  • 2020-12-20 13:40

    I tested with Android Studio 3.3.1 with SDK lollipop

    Keep an eye on the paths...

    Clean and build... I had a pretty hard time, thinking it's not working, but after deleting the file and Re-add, and few clean and build, I got it working strange...

    1. This works ( note the tick and its path)

    2. Here is proof

    0 讨论(0)
  • 2020-12-20 13:42
    int max = getContext().getResources().getInteger(R.integer.quantity_length);
    
    0 讨论(0)
提交回复
热议问题