کانال بله, جهت پشتیبانی و اطلاع رسانی کانال بله, جهت پشتیبانی و اطلاع رسانی
عضویت

آموزش نحوه ی استفاده از ‏action bar‏ اندروید‎ قسمت هشتم

 

clip_image001

دوره آموزش برنامه نویسی اندروید

کلیه حقوق مادی و معنوی این مقاله متعلق به آموزشگاه تحلیل داده می باشد و هر گونه استفاده غیر قانونی از آن پیگرد قانونی دارد.

 

آموزش نحوه ی استفاده از ‏action bar‏ اندروید قسمت هشتم

 راه اندازی برنامه و تست action bar آن

برنامه ی کاربردی خود را اجرا کرده و از کارایی هر دو action اطمینان کسب کنید (آیا action ها را می توان انتخاب کرد و در صورت انتخاب آن ها، عملیات لازم اتفاق می افتد یا خیر). بررسی کنید آیا در صورت انتخاب درایه های متفاوت info message مورد نظر نمایش داده می شود یا خیر.

برنامه در حال اجرا

نکته

چنانچه برنامه ی شبیه ساز یا دستگاه اندروید شما دارای Options menu button بود، در آن صورت overflow menu را مشاهده نمی کنید، کلید Option را زده تا action دوم پدیدار گردد.

حذف دکمه ی بازسازی (refresh button)

چنانچه در fragment layout خود دکمه ی باسازی دارید که دیگر به آن نیازی نیست، می توانید آن را حذف کنید.

11. تمرین : استفاده از contextual action mode

پروژه ای به نام de.vogella.android.socialapp ایجاد کرده و activity آن را OverviewActivity نام گذاری کنید.

یک عنصر EditText به فایل طرح بندی main.xml خود اضافه کنید.

xml version="1.0" encoding="utf-8" ?>

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"

               android:layout_width="match_parent"

               android:layout_height="match_parent"

               android:orientation="vertical">

        <edittext android:id="@+id/myView"

                    android:layout_width="match_parent"

                    android:layout_height="wrap_content"

                    android:ems="10">

                <requestfocus />

            EditText>

linearlayout>

یک menu XML resource که اسم فایل (filename) آن contextual.xml است، ایجاد کنید.

xml version="1.0" encoding="utf-8" ?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/toast"

           android:title="Toast">

            item>

menu>

Activity خود را بر اساس قالب / template پیش فرض اصلاح کنید (مشابه مثال زیر).

package de.vogella.android.socialapp;
import android.app.Activity;

import android.os.Bundle;

import android.view.ActionMode;

import android.view.Menu;

import android.view.MenuInflater;

import android.view.MenuItem;

import android.widget.Toast;

public class OverviewActivity extends Activity {

 
protected Object mActionMode;
  @Override
 
public void onCreate(Bundle savedInstanceState) {
   
super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

   
// define the contextual action mode
    View view = findViewById(R.id.myView);
    view.setOnLongClickListener(
new View.OnLongClickListener() {
     
// called when the user long-clicks on someView
     
public boolean onLongClick(View view) {
       
if (mActionMode != null) {
         
return false;
        }

       
// start the CAB using the ActionMode.Callback defined above
        mActionMode = OverviewActivity.this
           .startActionMode(mActionModeCallback);

        view.setSelected(
true);
       
return true;
      }

    });

  }

  @Override
 
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();

    inflater.inflate(R.menu.mainmenu، menu);

   
return true;
  }


  @Override
 
public boolean onOptionsItemSelected(MenuItem item) {
    Toast.makeText(
this، "Just a test"، Toast.LENGTH_SHORT).show();
   
return true;
  }

  private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() ‎‎{
    // Called when the action mode is created; startActionMode() was called
    public boolean onCreateActionMode(ActionMode mode، Menu menu) {
     
// inflate a menu resource providing context menu items
      MenuInflater inflater = mode.getMenuInflater();

     
// assumes that you have "contexual.xml" menu resources
      inflater.inflate(R.menu.contextual، menu);
     
return true;
    }

    // called each time the action mode is shown. Always called after
   
// onCreateActionMode، but
    // may be called multiple times if the mode is invalidated.
    public boolean onPrepareActionMode(ActionMode mode، Menu menu) {
     
return false; // Return false if nothing is done
    }
    // called when the user selects a contextual menu item
    public boolean onActionItemClicked(ActionMode mode، MenuItem item) {
      switch (item.getItemId()) {

      case R.id.toast:

        Toast.makeText(OverviewActivity.
this، "Selected menu"،
            Toast.LENGTH_LONG).show();

        mode.finish();
// Action picked، so close the CAB
        return true;
      default:

       
return false;
      }

    }

    // called when the user exits the action mode
    public void onDestroyActionMode(ActionMode mode) {
      mActionMode =
null;
    }

  };

}

پس از اجرای مثال بالا و long press کردن EditView، contextual action bar مورد نظر نمایان می گردد.

نمایش Contextual ActionBar

1394/07/27 4505 2420
رمز عبور : tahlildadeh.com یا www.tahlildadeh.com
نظرات شما

نظرات خود را ثبت کنید...