Help us understand the problem. What is going on with this article?

【Android/Kotlin】UIまとめ

More than 1 year has passed since last update.

目次

  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
kiyo1219
こんにちは、KIYOです。(@hayaoki1010) エンジニア2年目、備忘録です。 java / android / cloud (法人営業→SES→モバイル開発)
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away