Android में TextView डेटा केवल अंक हैं या नहीं वैलिडेट करना
यह उदाहरण दर्शाता है कि Android में TextView डेटा केवल अंक हैं या नहीं कैसे वैलिडेट करें।
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2 - लेआउट फ़ाइल में कोड जोड़ें
res/layout/activity_main.xml में निम्नलिखित कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:textSize="30sp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
उपरोक्त कोड में, हमने अंक दिखाने के लिए TextView लिया है।
चरण 3 - MainActivity.java में कोड जोड़ें
src/MainActivity.java में निम्नलिखित कोड जोड़ें:
package com.example.myapplication;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.List;
public class MainActivity extends AppCompatActivity {
TextView textView;
Location location;
List<Address> addresses;
Geocoder geocoder;
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
textView.setText("123456789");
Boolean isDigit = TextUtils.isDigitsOnly(textView.getText());
Toast.makeText(MainActivity.this, "" + isDigit, Toast.LENGTH_LONG).show();
}
}
एप्लिकेशन चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फ़ाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और डिवाइस पर आउटपुट देखें।