آموزش استفاده از 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="14dp" android:layout_marginTop="18dp" 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="130dp" 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="61dp" android:ems="10" android:text="@string/enter_text" android:inputType="text" /> </RelativeLayout>
و بعد از اون کدهای زیر رو داخل مسیر res/values/strings.xml قرار بدید
xml version="1.0" 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="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.demo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="22" /> <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-5d3440baae0aa438035250/]
خب حالا رو دکمه run کلیک کنید و صبر کنید تا پروژه Demo رو روی شبیه ساز AVD نصب و اجرا کنه .
فرض رو بر این قرار می دهیم که شما تنظیمات مربوط به AVD تون رو درست انجام دادید و AVD بدون هیچ مشکلی کار می کنه ،
به همین خاطر دیگه قسمت مربوط به اون رو توضیح نمیدم و میرم سراغ خروجی کارمون .
اگه همه مراحل رو درست اومده باشید و همه چیز درست پیش رفته باشه ؛ باید بتونید روی شبیه ساز AVD تون ، خروجی بگیرید و کار رو به اتمام برسانید