Android
 Computer >> コンピューター >  >> プログラミング >> Android

AndroidでEditTextのMIN値とMAX値を定義するにはどうすればよいですか?


この例は、AndroidでEditTextのMIN値とMAX値を定義する方法について示しています。

ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。

ステップ2 −次のコードをres / layout / activity_main.java

に追加します。
<? xml version= "1.0" encoding= "utf-8" ?>
<RelativeLayout xmlns: android = "https://schemas.android.com/apk/res/android"
   xmlns: tools = "https://schemas.android.com/tools"
   android :layout_width= "match_parent"
   android :layout_height= "match_parent"
   android :layout_margin= "16dp"
   tools :context= ".MainActivity" >
   <EditText
      android :id= "@+id/editText"
      android :layout_width= "match_parent"
      android :layout_height= "wrap_content"
      android :hint= "Enter number between 1-99"
      android :inputType= "number"
      tools :ignore= "Autofill" />
</RelativeLayout>

ステップ3 −次のコードをsrc / MinMaxFilter.java

に追加します
package app.tutorialspoint.com.sample ;
import android.text.InputFilter ;
import android.text.Spanned ;
public class MinMaxFilter implements InputFilter {
   private int mIntMin , mIntMax ;
   public MinMaxFilter ( int minValue , int maxValue) {
      this . mIntMin = minValue ;
      this . mIntMax = maxValue ;
   }
   public MinMaxFilter (String minValue , String maxValue) {
      this . mIntMin = Integer. parseInt (minValue) ;
      this . mIntMax = Integer. parseInt (maxValue) ;
   }
   @Override
   public CharSequence filter (CharSequence source , int start , int end , Spanned dest ,int dstart , int dend) {
      try {
         int input = Integer. parseInt (dest.toString() + source.toString()) ;
         if (isInRange( mIntMin , mIntMax , input))
            return null;
      } catch (NumberFormatException e) {
         e.printStackTrace() ;
      }
      return "" ;
   }
   private boolean isInRange ( int a , int b , int c) {
      return b > a ? c >= a && c <= b : c >= b && c <= a ;
   }
}

ステップ4 −次のコードをsrc / MainActivity.java

に追加します
package app.tutorialspoint.com.sample ;
import android.os.Bundle ;
import android.support.v7.app.AppCompatActivity ;
import android.text.InputFilter ;
import android.widget.EditText ;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate (Bundle savedInstanceState) {
      super .onCreate(savedInstanceState) ;
      setContentView(R.layout. activity_main ) ;
      EditText editText = findViewById(R.id. editText ) ;
      editText.setFilters( new InputFilter[]{ new MinMaxFilter( "1" , "99" )}) ;
   }
}

ステップ5 −次のコードをandroidManifest.xmlに追加します

<? xml version= "1.0" encoding= "utf-8" ?>
<manifest xmlns: android = "https://schemas.android.com/apk/res/android"
   package= "app.tutorialspoint.com.sample" >
   <uses-permission android :name= "android.permission.CALL_PHONE" />
   <application
      android :allowBackup= "true"
      android :icon= "@mipmap/ic_launcher"
      android :label= "@string/app_name"
      android :roundIcon= "@mipmap/ic_launcher_round"
      android :supportsRtl= "true"
      android :theme= "@style/AppTheme" >
      <activity android :name= ".MainActivity" >
         <intent-filter>
            <action android :name= "android.intent.action.MAIN" />
            <category android :name= "android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 android studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[実行]アイコンをクリックします。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します–

AndroidでEditTextのMIN値とMAX値を定義するにはどうすればよいですか?


  1. ExcelのMIN、Max、およびAVERAGE関数の使用方法

    機能 Excelのセルの範囲で数学計算を実行します。最も一般的な機能 SUM 、平均 、カウント 、 MIN 、および MAX 。個人がデータの平均、最小、または最大を見つけたい場合は、平均を使用できます。 、 MIN 、およびMAX関数 。この投稿では、Excelで平均、最小、最大を計算する方法を紹介します。 ExcelのAVERAGE、MIN、およびMAX関数 平均 :特定のセルの数値を検索し、1つ以上の値の平均値を返します。数字を含む名前、配列、参照にすることができます。 MIN :セルの範囲で最小値を検索します。 最大 :セルの範囲で最大値を検索します。

  2. PCとAndroid用のGoogleセーフサーチをオンにする方法

    インターネットは世界の情報スーパーハイウェイです。つまり、毎日の消費にオンラインで利用できるたくさんの便利なものに加えて、オンラインでニーズを検索するときに遠く離れたままにしておきたい、同じ量の不快で危険なコンテンツがあります。その望ましくないコンテンツは、Google検索履歴に表示されて、オンラインプロファイルデータの一部になることから離れた1つの偶発的な検索用語です。幸い、Googleは、検索エンジンを使用しているときに、不快なコンテンツを除外する選択肢を提供しています。 PCでの検索コンテンツのフィルタリング 最初にメインのGmailページに移動して、Googleアカウントにアクセスし