fbpx

Создание Activity

В этом уроке мы создадим Activity, которая умеет добавлять слова в БД.

Для этого сначала добавьте следующие строковые ресурсы в  values/strings.xml:

<string name="hint_word">Введите слово</string>
<string name="button_save">Сохранить</string>
<string name="empty_not_saved">Невозможно сохранить, так как значение пустое.</string>

И добавьте цвета в value/colors.xml:

<color name="buttonLabel">#d3d3d3</color>

Кроме этого, давайте добавим размеры, для этого

  1. Выберите модуль app в окне Project
  2. Выберите File > New > Android Resource File
  3. Из доступных категорий выберите Dimension
  4. Выберите имя: dimens

Вставьте значения размеров в values/dimens.xml как показано ниже:

<dimen name="small_padding">6dp</dimen>
<dimen name="big_padding">16dp</dimen>

После этого создайте новую Activity

  1. Выберите File > New > Activity > Empty Activity
  2. Введите название NewWordActivity.
  3. Не забудьте проверить что новая Activity появилась в Android Manifest!
<activity android:name=".NewWordActivity"></activity>

Обновите  activity_new_word.xml как показано ниже:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent">

   <EditText
       android:id="@+id/edit_word"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:fontFamily="sans-serif-light"
       android:hint="@string/hint_word"
       android:inputType="textAutoComplete"
       android:padding="@dimen/small_padding"
       android:layout_marginBottom="@dimen/big_padding"
       android:layout_marginTop="@dimen/big_padding"
       android:textSize="18sp" />

   <Button
       android:id="@+id/button_save"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@color/colorPrimary"
       android:text="@string/button_save"
       android:textColor="@color/buttonLabel" />

</LinearLayout>

Далее, обновите код в новой Activity как показано ниже:

class NewWordActivity : AppCompatActivity() {

    private lateinit var editWordView: EditText

    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_new_word)
        editWordView = findViewById(R.id.edit_word)

        val button = findViewById<Button>(R.id.button_save)
        button.setOnClickListener {
            val replyIntent = Intent()
            if (TextUtils.isEmpty(editWordView.text)) {
                setResult(Activity.RESULT_CANCELED, replyIntent)
            } else {
                val word = editWordView.text.toString()
                replyIntent.putExtra(EXTRA_REPLY, word)
                setResult(Activity.RESULT_OK, replyIntent)
            }
            finish()
        }
    }

    companion object {
        const val EXTRA_REPLY = "com.example.android.wordlistsql.REPLY"
    }
}