gpt4 book ai didi

android - 如何扩展android中的基本 Activity ?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:39:00 25 4
gpt4 key购买 nike

我在 android Activity 中创建了一个选项菜单。我有很多 Activity ,我希望在所有 Activity 中使用相同的选项菜单。我知道我必须创建一个基本 Activity 并扩展它,但我不知道如何做。这是我的主要 Activity 代码....

            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);
}
}
}

最佳答案

创建一个 Activity ,它可以是一个扩展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 how to create my own Activity and extend it?问题

Common Header in different activities using BaseActivity in android

Android: Base Activity class Example了解更多。

希望对您有所帮助。

关于android - 如何扩展android中的基本 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18607039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com