[Android Kotlin 기초] 3-2. Add user interactivity
Add user interactivity
Objectives
- Obtaining user input from an
EditText. - Setting text to a
TextViewfrom aEditText. -
Working with
ViewandViewGroup - Changing the visibility of a
View
Adding an EditText for text input
-
Drag and drop
EditTextto the panel.<EditText android:layout_width="match_parent" android:layout_height="wrap_content" /> -
Style the
EditTextby adding a hint, changing the text alignment, changing the style to theNameStyle, and setting the input type.<EditText style="@style/NameStyle" android:id="@+id/nickname_edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPersonName" android:hint="@string/what_is_your_nickname"/><string name="what_is_your_nickname">What is your NickName?</string><style name="NameStyle"> <item name="android:layout_marginTop">@dimen/layout_margin</item> <item name="android:fontFamily">@font/roboto</item> <item name="android:paddingTop">@dimen/small_padding</item> <item name="android:textColor">@android:color/black</item> <item name="android:textSize">@dimen/text_size</item> </style> -
Adding
Buttonand style it<string name="done">Done</string><Button android:id="@+id/done_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/done"/><Button style="@style/Widget.AppCompat.Button.Colored" android:id="@+id/done_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="@dimen/layout_margin" android:fontFamily="@font/roboto" android:text="@string/done" />Widget.AppCompat.Button.Coloredchanges the button color to theColorAccent.
<color name="colorAccent">#76bf5e</color> -
Adding a
TextViewto display the nickname<TextView style="@style/NameStyle" android:id="@+id/nickname_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAlignment="center" android:visibility="gone" android:text="" /> -
Adding a click listener to the DONE
ButtonandTextViewView.OnClickListenerthat is defined inViewclass.
public static interface View.OnClickListener { void onClick(View v); }button.setOnClickListener(object:View.OnClickListener { override fun onClick(v:View){ clickEvent(it) } }) // or button.setOnClickListener{ clickEvent(it) } fun clickEvent(view:View){ // TODO: ... }<Button android:id="@+id/done_button" android:text="@string/done" ... android:onClick="clickEvent"/>private fun addNickname(view: View) { val editText = findViewById<EditText>(R.id.nickname_edit) val nicknameTextView = findViewById<TextView>(R.id.nickname_text) nicknameTextView.text = editText.text editText.visibility = View.GONE view.visibility = View.GONE nicknameTextView.visibility = View.VISIBLE // Hide keyboard ... }override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) ... findViewById<Button>(R.id.done_button).setOnClickListener { addNickname(it) }- How to show / hide the soft keyboard
// Show the keyboard. val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.showSoftInput(editText, 0) // Hide the keyboard. val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(view.windowToken, 0) -
Homework
- What is
EditTexta subclass of?ViewLinearLayoutTextView(O)Button
- Which of the following
visibilityattribute values, if set on a view, makes it so the view is hidden and does not take up any space in the layout?visibleinvisiblegone(O)hide
- For
EditTextviews, it’s not a good practice to provide hints, because hints clutter the input field. True or false?- True
- False (0)
- Which one of the following statements is true about
Buttonviews?- A
Buttonview is a view group. - You can only have three
Buttonviews per screen. Buttonviews are clickable, and on click, the attached click listener performs an action. (O)Buttonis an extension ofImageView.
- A
- What is