LoginSignup
1
1

More than 3 years have passed since last update.

【Android/Kotlin】UIまとめ

Posted at

目次

  1. パスワード非表示
  2. addTextChangedListenerの書き換え
  3. enum書き換え

1. パスワード非表示

undefined

  • java
MainActivity.java
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);  
  • kotlin
activity_main.xml
<EditText
        android:id="@+id/edit_text"
        android:layout_width="200dp"
        android:layout_height="60dp"
        android:inputType="textPassword"></EditText>

2. addTextChangedListenerの書き換え

  • java
MainActivity.java
  editText.addTextChangedListener(new TextWatcher() { 
       @Override 
       public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
      }
       @Override 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 
         calculatePasswordStrength(s.toString()); 
       } 
       @Override 
       public void afterTextChanged(Editable s) { 
     } 
     }); 
   } 
  • kotlin
MainActivity.kt
 edit_text.addTextChangedListener(object : TextWatcher{
            override fun afterTextChanged(p0: Editable?) {
            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
})

enum書き換え

  • java
MainActivity.java
 WEAK(R.string.weak, R.color.weak), 
 MEDIUM(R.string.medium, R.string.medium), 
 STRONG(R.string.strong, R.string.strong),    
 VERY_STRONG(R.string.very_strong, R.string.very_strong); 
  • kotlin
  • companion objectは列挙のあとに書かないとエラーになる。
MainActivity.kt
WEAK {
        override fun stringId(): Int = R.string.weak
        override fun colorId(): Int = R.color.weak
    },
    MEDIUM {
        override fun stringId(): Int = R.string.medium
        override fun colorId(): Int = R.color.medium
    },
    STRONG {
        override fun stringId(): Int = R.string.strong
        override fun colorId(): Int = R.color.strong
    },
    VERY_STRONG {
        override fun stringId(): Int = R.string.very_strong
        override fun colorId(): Int = R.color.very_strong
    };

    abstract fun stringId(): Int
    abstract fun colorId(): Int
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1