如何在安卓中扩展基本活动?

4

我在 Android 活动中创建了一个选项菜单。我有许多活动,希望所有活动都有相同的选项菜单。我知道我需要创建一个基础活动并扩展它,但我不知道如何操作。这是我的主要活动代码...

            package com.officextracts.kaspersky;

        import android.os.Bundle;
        import android.app.Activity;
        import android.content.Intent;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.View;
        import android.widget.Button;
        import android.widget.ImageButton;
        import android.widget.Toast;

        public class MainActivity extends Activity implements View.OnClickListener
        {

            Button Button01;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                Button01 = (Button)findViewById(R.id.Button01);
                Button01.setOnClickListener(this);
            }

            private void button1Click()
            {
                startActivity(new Intent("com.officextracts.kaspersky.Retail_products"));

            }

            public void onClick(View v) {
                switch (v.getId())
                {
                case R.id.Button01:
                    button1Click();
                    break;
                }


            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.layout.menu, menu);
                return true;
            }
            @Override
            public boolean onOptionsItemSelected(MenuItem item)
            {

                switch (item.getItemId())
                {
                case R.id.menu_home:
                    // Single menu item is selected do something
                    // Ex: launching new activity/screen or show alert message
                    Toast.makeText(MainActivity.this, "Home Is selected", Toast.LENGTH_SHORT).show();
                    return true;

                case R.id.menu_krp:
                    Toast.makeText(MainActivity.this, "Kaspersky Retail Products", Toast.LENGTH_SHORT).show();
                    return true;

                case R.id.menu_kep:
                    Toast.makeText(MainActivity.this, "Kaspersky Endpoint Products", Toast.LENGTH_SHORT).show();
                    return true;

                case R.id.menu_fkr:
                    Toast.makeText(MainActivity.this, "Find Kaspersky Resaller", Toast.LENGTH_SHORT).show();
                    return true;

                case R.id.menu_sales:
                    Toast.makeText(MainActivity.this, "Contact Kaspersky Sales", Toast.LENGTH_SHORT).show();
                    return true;

                case R.id.menu_crs:
                    Toast.makeText(MainActivity.this, "Contact Retail Support", Toast.LENGTH_SHORT).show();
                    return true;

                case R.id.menu_ces:
                    Toast.makeText(MainActivity.this, "Contact Enterprise Support", Toast.LENGTH_SHORT).show();
                    return true; 

                case R.id.menu_coo:
                    Toast.makeText(MainActivity.this, "Contact Our Office", Toast.LENGTH_SHORT).show();
                    return true;


                case R.id.menu_sms:
                    Toast.makeText(MainActivity.this, "SMS for Support", Toast.LENGTH_SHORT).show();
                    return true;


                case R.id.menu_email:
                    Toast.makeText(MainActivity.this, "Email Support", Toast.LENGTH_SHORT).show();
                    return true;            

                case R.id.menu_exit:
                    finish();
                    System.exit(0);                

                default:
                    return super.onOptionsItemSelected(item);
                }
            }    
        }

https://dev59.com/K2oy5IYBdhLWcg3wBJkx#8821459 - Tushar Pandey
2个回答

6

创建一个可以作为Activity的抽象类扩展的活动。现在每个新的Activity都必须扩展该类。例如:

//Your base class, where you can have your Action bar and other the other methods which you want all your other classes to inherit
public abstract class AbstractActivity extends Activity{}
 

// You all other classes
public class FirstActivity extends AbstractActivity{}
public class SecondActivity extends AbstractActivity{}

请看这个问题:如何创建并扩展自己的 Android Activity?

在不同的 activities 中使用 BaseActivity 实现通用的头部,请参考这个链接:Android 中使用 BaseActivity 在不同的 activities 中实现通用的头部

可以参考这篇文章:Android: Base Activity class Example

希望对您有所帮助。


0

是的,这是可能的。

您只需在其他活动中扩展 主活动

您的流程将会是: MainActivity(超类)->OtherActivities(子类)//它扩展了MainActivity


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接