有人可以帮我实现一个操作栏吗?

发布于 2024-12-19 07:18:01 字数 46 浏览 0 评论 0原文

有人可以帮助我从头到尾实现一个操作栏,包括帮助我在我的应用程序中使用操作栏吗?

Can some one help me implement an action bar from start to finish including helping me getting one working in my app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪淡了忧伤 2024-12-26 07:18:01
import android.app.Activity;
import android.os.Bundle;
import android.app.ActionBar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

 /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    CreateMenu(menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    return MenuChoice(item);
}

private void CreateMenu(Menu menu)
{
    MenuItem mnu1 = menu.add(0, 0, 0, "Item 1");
    {
        mnu1.setAlphabeticShortcut('a');
        mnu1.setIcon(R.drawable.ic_launcher);
    }
    MenuItem mnu2 = menu.add(0, 1, 1, "Item 2");
    {
        mnu2.setAlphabeticShortcut('b');
        mnu2.setIcon(R.drawable.ic_launcher);
    }
    MenuItem mnu3 = menu.add(0, 2, 2, "Item 3");
    {
        mnu3.setAlphabeticShortcut('c');
        mnu3.setIcon(R.drawable.ic_launcher);
    }
    MenuItem mnu4 = menu.add(0, 3, 3, "Item 4");
    {
        mnu4.setAlphabeticShortcut('d');
    }
    menu.add(0, 3, 3, "Item 5");
    menu.add(0, 3, 3, "Item 6");
    menu.add(0, 3, 3, "Item 7");
}
private boolean MenuChoice(MenuItem item)
{
    switch (item.getItemId()) {
    case 0:
        Toast.makeText(this, "You clicked on Item 1",
                Toast.LENGTH_LONG).show();
        return true;
    case 1:
        Toast.makeText(this, "You clicked on Item 2",
                Toast.LENGTH_LONG).show();
        return true;
    case 2:
        Toast.makeText(this, "You clicked on Item 3",
                Toast.LENGTH_LONG).show();
        return true;
    case 3:
        Toast.makeText(this, "You clicked on Item 4",
                Toast.LENGTH_LONG).show();
        return true;
    case 4:
        Toast.makeText(this, "You clicked on Item 5",
                Toast.LENGTH_LONG).show();
        return true;
    case 5:
        Toast.makeText(this, "You clicked on Item 6",
                Toast.LENGTH_LONG).show();
        return true;
    case 6:
        Toast.makeText(this, "You clicked on Item 7",
                Toast.LENGTH_LONG).show();
        return true;
    }
    return false;
}
}
import android.app.Activity;
import android.os.Bundle;
import android.app.ActionBar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

 /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    CreateMenu(menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    return MenuChoice(item);
}

private void CreateMenu(Menu menu)
{
    MenuItem mnu1 = menu.add(0, 0, 0, "Item 1");
    {
        mnu1.setAlphabeticShortcut('a');
        mnu1.setIcon(R.drawable.ic_launcher);
    }
    MenuItem mnu2 = menu.add(0, 1, 1, "Item 2");
    {
        mnu2.setAlphabeticShortcut('b');
        mnu2.setIcon(R.drawable.ic_launcher);
    }
    MenuItem mnu3 = menu.add(0, 2, 2, "Item 3");
    {
        mnu3.setAlphabeticShortcut('c');
        mnu3.setIcon(R.drawable.ic_launcher);
    }
    MenuItem mnu4 = menu.add(0, 3, 3, "Item 4");
    {
        mnu4.setAlphabeticShortcut('d');
    }
    menu.add(0, 3, 3, "Item 5");
    menu.add(0, 3, 3, "Item 6");
    menu.add(0, 3, 3, "Item 7");
}
private boolean MenuChoice(MenuItem item)
{
    switch (item.getItemId()) {
    case 0:
        Toast.makeText(this, "You clicked on Item 1",
                Toast.LENGTH_LONG).show();
        return true;
    case 1:
        Toast.makeText(this, "You clicked on Item 2",
                Toast.LENGTH_LONG).show();
        return true;
    case 2:
        Toast.makeText(this, "You clicked on Item 3",
                Toast.LENGTH_LONG).show();
        return true;
    case 3:
        Toast.makeText(this, "You clicked on Item 4",
                Toast.LENGTH_LONG).show();
        return true;
    case 4:
        Toast.makeText(this, "You clicked on Item 5",
                Toast.LENGTH_LONG).show();
        return true;
    case 5:
        Toast.makeText(this, "You clicked on Item 6",
                Toast.LENGTH_LONG).show();
        return true;
    case 6:
        Toast.makeText(this, "You clicked on Item 7",
                Toast.LENGTH_LONG).show();
        return true;
    }
    return false;
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文