آموزش اندروید: View های اندروید

آموزش اندروید: View های اندروید
فهرست مقاله [نمایش]

    هر اپلیکیشنی، یک شکلی رو به عنوان رابط کاربری در نظر می گیرد. در اندروید،این کار توسط View و ViewGroup انجام میشود.در این مقاله میخواهیم با View های اندروید کار کنیم.

     در دوره رایگان آموزش  برنامه نویسی اندروید با کاتلین  با View های اندروید بیشتر آشنا خواهید شد.
     

     

    View های عمومی از textview  شروع میکنیم. برای نمایش متن از این ویو استفاده میکنیم که یکی از پر کاربرد ترین View های اندرویداست.
     

    <TextView
        android:text="Bugeto.net"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    

    Button : برای نمایش دکمه های قابل کلیک از این ویو استفاده می کنیم.
     

    <Button
        android:text="Click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    

    Image button : شبیه به button  است با این تفاوت که یک تصویر را هم میتواند نمایش دهد.
     

    <ImageButton
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    
    

    EditText : این View یک زیر کلاس از text view است که امکان ویرایش متن را به کاربر می دهد.
     

    <EditText
        android:src="@mipmap/ic_launcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    

    box : یک نوع خاص از دکمه است که دو حالت تیک شده و تیک نشده دارد.

    <CheckBox
        android:text="is online"
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    
    

     

    Radio button : دو حالت انتخاب شده و انتخاب نشده دارد.

    نکته این ویو این است که بعد از انتخاب نمیتوانیم آن را تغییر حالت بدهیم.

    <RadioButton
        android:text="Android Kotlin"
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    

     

    Radio group : از این ویو برای گروه بندی یک یا چند radio button استفاده میشود و ما فقط میتوانیم یکی از radio button ها را انتخاب کنیم.

    <RadioGroup
        android:src="@mipmap/ic_launcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/rb_kotlin"
            android:text="Kotlin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <RadioButton
            android:id="@+id/rb_java"
            android:text="Java"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RadioGroup>
    
    

     

    Toggle button: این View وضعیت تیک خوردن یا نخوردن را با استفاده از رنگ مشخص می کند.

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    

    AutoCompliteTextView : این ویو شبیه به edittext هست در واقع  یک زیر کلاس از Edittext است با این تفاوت که یک لیست از داده ها دارد که موقع تایپ کردن به کاربر نمایش می دهد.

    در layout این view را تعریف میکنیم.

      برای یادگیری Layoutها می توانید مقاله آموزش Layoutها در اندروید را مطالعه کنید.

    <AutoCompleteTextView
        android:id="@+id/autoTv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    
    

    بعد از انجام این کار در کلاس اکتیویتی کد های زیر را می نویسیم.

    var names = arrayOf("Maysam", "Ali", "Hossein", "mohammad", "Amir", "Alireza")
    private var tvCountries:AutoCompleteTextView?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    
        setData() //269173
    }
    
    private fun setData() {
        tvCountries = findViewById<AutoCompleteTextView>(R.id.autoTv)
        var adapter = ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, names)
        tvCountries?.threshold = 3 //این خط برای این است که بگوییم کاربر بعد از تایپ چند کاراکتر نتیجه را ببیند
        tvCountries?.setAdapter(adapter)
    }
    

     

    View های picker

    انتخاب ساعت و تاریخ یکی از کارهای عمومی توی اپلیکیشن های اندروید است.

    این کار به وسیله time picker و date picker انجام میشود.
     
    سخن پایانی :
    در این مقاله سعی شده شما را با view  های اندروید آشنا کنیم.


    • نویسنده: میثم بابائی

    ارسال دیدگاه

    برای افزودن دیدگاه خود، نیاز است ابتدا وارد حساب کاربری‌تان شوید


    دیدگاه کاربران