آموزش استفاده از Edit text در اندروید استودیو:
Edit text کامپوننتی است که از آن برای گرفتن متنی از کاربر استفاده می شود و اسم ان بدان جهت Edit text نامیده شده است
که به کاربر این امکان را می دهد که متنی را که نوشته است را ادیت و ویرایش کند و تفاوتش با Text view این است که در Text view
قابلیت ویرایش متن نوشته شده وجود ندارد ولی در کامپوننت Edit text برعکس است البته که ادیت تکست تمام خصوصیات و ویژگی های تکست ویو را دارد.
و اما کد نویسی برای ایجاد کامپوننت ادیت تکست:
مرحله شماره ۱
کدهای زیر رو داخل مسیر res/layout/activity_main.xmlقرار بدید
۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ ۱۱ ۱۲ ۱۳ ۱۴ ۱۵ ۱۶ ۱۷ ۱۸ ۱۹ ۲۰ ۲۱ ۲۲ ۲۳ ۲۴ ۲۵ ۲۶ ۲۷ ۲۸ ۲۹ ۳۰ ۳۱ ۳۲ ۳۳ ۳۴ ۳۵ ۳۶ ۳۷ ۳۸ ۳۹ ۴۰ |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="۱۴dp" android:layout_marginTop="۱۸dp" android:text="@string/example_edittext" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="۱۳۰dp" android:text="@string/show_the_text" /> <EditText android:id="@+id/edittext" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button" android:layout_below="@+id/textView1" android:layout_marginTop="۶۱dp" android:ems="۱۰" android:text="@string/enter_text" android:inputType="text" /> </RelativeLayout> |
و بعد از اون کدهای زیر رو داخل مسیر res/values/strings.xml قرار بدید
۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ |
xml version="۱.۰" encoding="utf-8"; resources; string name="app_name">demo</string; string name="action_settings">Settings</string; string name="example_edittext">Example showing EditText</string; string name="show_the_text">Show the Text</string; string name="enter_text">text changes</string; /resources; |
و بعد از اون کدهای زیر رو داخل فایل Manifest.xml قرار بدید
۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ ۱۱ ۱۲ ۱۳ ۱۴ ۱۵ ۱۶ ۱۷ ۱۸ ۱۹ ۲۰ ۲۱ ۲۲ ۲۳ ۲۴ ۲۵ ۲۶ ۲۷ ۲۸ ۲۹ |
<?xml version="۱.۰" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.demo" android:versionCode="۱" android:versionName="۱.۰" > <uses-sdk android:minSdkVersion="۸" android:targetSdkVersion="۲۲" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.demo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
مرحله شماره ۲
و در مرحله دوم کدهای زیر رو داخل فایل جاواتون که در مسیر src/com.example.demo/MainActivity.javaقرار گرفته ، قرار بدید .
۱ |
[crayon-۶۰۰f00e466b12873905435-۵d3440baae0aa438035250/] |
خب حالا رو دکمه run کلیک کنید و صبر کنید تا پروژه Demo رو روی شبیه ساز AVD نصب و اجرا کنه .
فرض رو بر این قرار می دهیم که شما تنظیمات مربوط به AVD تون رو درست انجام دادید و AVD بدون هیچ مشکلی کار می کنه ،
به همین خاطر دیگه قسمت مربوط به اون رو توضیح نمیدم و میرم سراغ خروجی کارمون .
اگه همه مراحل رو درست اومده باشید و همه چیز درست پیش رفته باشه ؛ باید بتونید روی شبیه ساز AVD تون ، خروجی بگیرید و کار رو به اتمام برسانید