本文介绍了在Android中使用BaseActivity在不同的活动,公共头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望写code一次,在不同的活动中使用。我创建了一个基本活动类为。此外,在不同的活动的所有布局的报头相同。我已经做到了与&LT的帮助,包括布局> 标记。

I want write code once and use in different activities. I have created a Base Activity class for that . Also the header of all the layouts in different activities are same. I have done that with the help of the <include layout > tag.

现在的问题是我的 BaseActivity code没有运行。我想这第一次本身没有关于那么多的想法。

Now the Problem is my BaseActivity code is not running. I am trying this first time se don't have much idea about that.

1)的BaseActivity code是如下:

1.)The BaseActivity code is below :

package com.waheguru.app;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.Toast;

public abstract class BaseActivityMenu extends Activity {
    //action id
    private static final int ID_UP     = 1;
    private static final int ID_DOWN   = 2;
    private static final int ID_SEARCH = 3;
    private static final int ID_INFO   = 4;
    private static final int ID_ERASE  = 5;
    private static final int ID_OK     = 6;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.header);

        ActionItem nextItem     = new ActionItem(ID_DOWN, "Book", getResources().getDrawable(R.drawable.menu_down_arrow));
        ActionItem prevItem     = new ActionItem(ID_UP, "Bookmark", getResources().getDrawable(R.drawable.menu_up_arrow));
        ActionItem searchItem   = new ActionItem(ID_SEARCH, "Find", getResources().getDrawable(R.drawable.menu_search));
        ActionItem infoItem     = new ActionItem(ID_INFO, "Info", getResources().getDrawable(R.drawable.menu_info));
        ActionItem eraseItem    = new ActionItem(ID_ERASE, "Clear", getResources().getDrawable(R.drawable.menu_eraser));
        ActionItem okItem       = new ActionItem(ID_OK, "OK", getResources().getDrawable(R.drawable.menu_ok));

        //use setSticky(true) to disable QuickAction dialog being dismissed after an item is clicked
        prevItem.setSticky(true);
        nextItem.setSticky(true);

        //create QuickAction. Use QuickAction.VERTICAL or QuickAction.HORIZONTAL param to define layout
        //orientation
        final QuickAction quickAction = new QuickAction(this, QuickAction.VERTICAL);

        //add action items into QuickAction
        quickAction.addActionItem(nextItem);
        quickAction.addActionItem(prevItem);
        quickAction.addActionItem(searchItem);
        quickAction.addActionItem(infoItem);
        quickAction.addActionItem(eraseItem);
        quickAction.addActionItem(okItem);

        //Set listener for action item clicked
        quickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
            public void onItemClick(QuickAction source, int pos, int actionId) {
                ActionItem actionItem = quickAction.getActionItem(pos);

                //here we can filter which action item was clicked with pos or actionId parameter
                if (actionId == ID_SEARCH) {
                    Toast.makeText(getApplicationContext(), "Let's do some search action", Toast.LENGTH_SHORT).show();
                } else if (actionId == ID_INFO) {
                    Toast.makeText(getApplicationContext(), "I have no info this time", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), actionItem.getTitle() + " selected", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //set listnener for on dismiss event, this listener will be called only if QuickAction dialog was dismissed
        //by clicking the area outside the dialog.
        quickAction.setOnDismissListener(new QuickAction.OnDismissListener() {
            public void onDismiss() {
                Toast.makeText(getApplicationContext(), "Dismissed", Toast.LENGTH_SHORT).show();
            }
        });
        Button books=(Button)findViewById(R.id.book);
        books.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent intent=new Intent(ExampleActivity.this,List_of_books.class);
                startActivityForResult(intent, 0);
            }
        });
        //show on btn1
        Button btn1 = (Button) this.findViewById(R.id.menu);
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                quickAction.show(v);
            }
        });
    }
}

2)的活动扩展了基本活动

2.) The Activity extended the Base Activity

package com.waheguru.app;

import android.app.Activity;
import android.os.Bundle;

public class ABCActivity extends BaseActivityMenu  {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
    }
}

所以,任何一个可以帮助我,我做错了什么。

So can any one help me where I am doing something wrong.

推荐答案

有关这一点,你必须创建一个header.xml其中将包括每一个布局,为您的活动如下:

For this you have to create one header.xml which will be included in each and every layout for your activities as follows

header.xml

header.xml

<RelativeLayout>
  <TextView android:id="@+id/txtHeading"
      .... />
</RelativeLayout>

activity_main.xml

activity_main.xml

<RelativeLayout>
  <!-- include your header here -->
  <include layout="@layout/header"
     ... />

  <!-- Rest of your views -->

</RelativeLayout>

BaseActivity

BaseActivity

abstract class BaseActivity extends Activity {
  protected TextView txtHeading;
  public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
  }


  protected void setHeading(int resId) {
     if(txtHeading == null)
     txtHeading = findViewById(R.id.txtHeading);
     if(txtHeading != null)
       txtHeading.setText(resId);
  }
}

MainActivity

MainActivity

class MainActivity extends BaseActivity {
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      setHeading(R.string.heading_main);
   }
}

您可以把你想要的意见和管理共同的东西在BaseActivity或BaseListActivity。

You can put as many views you want and manage common things in BaseActivity or BaseListActivity.

这篇关于在Android中使用BaseActivity在不同的活动,公共头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 08:49