質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

2回答

3211閲覧

Android開発で数字入力必須のテキストフォームを作成する方法について

退会済みユーザー

退会済みユーザー

総合スコア0

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2020/02/20 10:11

前提・実現したいこと

Androidアプリ開発で、言語はKotlinで、入力必須のテキストフォームを作成しようとしています。

具体的には、ボタンをクリックすると別のページに移動するアプリで、フォームに何かしら文字が入力されていないと、次のページにいけなくなるようにしたいです。

警告方法は、ポップアップや、Webアプリなどにある「required」のようにフォーム下に警告が出るといった方法など、特に指定はなく、優先順位として開発の容易さを重視しています。

HTML

1<form action="xxx.php" method="post"> 2<fieldset> 3名前: 4<input type="text" name="yourname" required> ※入力必須<br> 5<input type="submit" value="送信"> 6</fieldset> 7</form>

発生している問題・エラーメッセージ

現状のコードだと、フォームに何も記入されてなくても次のページに移行してしまうのですが、入力必須にする方法を検索しても見つけられず、困っています。

ご存知の方がいらっしゃれば、教えてください。

該当のソースコード

「アプリの画面遷移とActivity間のデータ転送」の記事からフォーム指定を、Text>Numberに変えました。

画面遷移後のプログラムは、記事から変更ありません。

MainActivity.kt

kotlin

1package com.example.adefault 2 3import androidx.appcompat.app.AppCompatActivity 4import android.os.Bundle 5import android.app.Activity 6import android.content.Intent 7import android.util.Log 8import kotlinx.android.synthetic.main.activity_main.* 9 10class MainActivity : AppCompatActivity() { 11 12 companion object { 13 const val EXTRA_MESSAGE = "com.example.kotlinactivitydatatrans.MESSAGE" 14 } 15 16 private val RESULT_SUBACTIVITY = 1000 17 18 override fun onCreate(savedInstanceState: Bundle?) { 19 super.onCreate(savedInstanceState) 20 setContentView(R.layout.activity_main) 21 22 button.setOnClickListener { 23 if (editText.text != null) { 24 val intent = Intent(applicationContext, SubActivity::class.java) 25 val str = editText.text.toString() 26 Log.d("debug",str) 27 28 intent.putExtra(EXTRA_MESSAGE, str) 29 startActivityForResult(intent, RESULT_SUBACTIVITY) 30 31 editText.setText("") 32 33 } 34 } 35 } 36 37 // to get a result form SubActivity 38 override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) { 39 super.onActivityResult(requestCode, resultCode, intent) 40 41 if (resultCode == Activity.RESULT_OK && 42 requestCode == RESULT_SUBACTIVITY && intent != null) { 43 44 val res = intent.extras?.getString(EXTRA_MESSAGE)?: "" 45 textView.text = res 46 } 47 } 48}

activity_main.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:background="#dfe" 8 android:orientation="vertical" 9 tools:context=".MainActivity"> 10 11 <TextView 12 android:text="@string/main" 13 android:textSize="24sp" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:gravity="center_horizontal" 17 android:layout_marginTop="40dp" /> 18 19 <LinearLayout 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:orientation="horizontal"> 23 24 <TextView 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:layout_margin="20dp" 28 android:text="@string/to_sub" 29 android:textSize="24sp" /> 30 31 <EditText 32 android:id="@+id/editText" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:layout_weight="1" 36 android:ems="10" 37 android:hint="@string/hint1" 38 android:inputType="number" /> 39 </LinearLayout> 40 41 <LinearLayout 42 android:layout_width="match_parent" 43 android:layout_height="wrap_content" 44 android:orientation="horizontal"> 45 46 <TextView 47 android:layout_width="wrap_content" 48 android:layout_height="wrap_content" 49 android:layout_margin="20dp" 50 android:text="@string/from_sub" 51 android:textSize="24sp" /> 52 53 <TextView 54 android:id="@+id/textView" 55 android:layout_width="220dp" 56 android:layout_height="wrap_content" 57 android:layout_margin="20dp" 58 android:textSize="24sp" /> 59 60 </LinearLayout> 61 62 <Button 63 android:id="@+id/button" 64 android:text="@string/move" 65 android:textSize="24sp" 66 android:layout_width="match_parent" 67 android:layout_height="wrap_content" 68 android:gravity="center" 69 android:layout_margin="20dp" /> 70 71</LinearLayout>

試したこと

inputTypeに指定できる属性値といったフォームの規定に関する記事などを読んでいますが、入力必須の設定方法は見つけられていない状態です。

補足情報(FW/ツールのバージョンなど)

Android Studio 3.5.3

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

既に同じような回答をされている方もいらっしゃいますが、

if (editText.text != null)

ではなく
if (editText.text != null && !editText.text.toString().isEmpty())
としないと、未入力の場合、editText.textの値はnullではないため、画面遷移してしまいます。

警告は、else文を追加して、
Toast.makeText(applicationContext, "数値を入力して下さい", Toast.LENGTH_SHORT).show()
などとしてみてはいかがでしょうか?
画面下部にメッセージが表示されます。

Toastの簡単な解説
https://akira-watson.com/android/kotlin/toast-simple.html

投稿2020/02/20 10:31

編集2020/02/20 10:33
KojimaYuuki

総合スコア24

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

0

editText.text != null && !editText.text.equals("")

未入力の場合文字列は空文字列が送信されます。

投稿2020/02/20 10:17

riko111

総合スコア149

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問