Android switch语句麻烦

发布于 2024-10-17 11:56:03 字数 2110 浏览 10 评论 0原文

我是 Java 和 Android 新手,我想知道是否可以在这种情况下使用 Intent 来调出一个新背景,后跟用户单击的项目的文本(例如;他们单击打开的联系人,我告诉他们什么是打开的联系人)或者还有其他方法吗?

public class ListActivityExample extends ListActivity{
static final String[] ACTIVITY_CHOICES = new String[] {
    "Open Website Example",
    "Open Contacts",
    "Open Phone Dialer Example",
    "Search Google Example",
    "Start Voice Command"
};
final String searchTerms = "superman";

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, ACTIVITY_CHOICES));
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    getListView().setTextFilterEnabled(true);
    getListView().setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3){
            switch(arg2) {
            case 0: //opens web browser and navigates to given website
                 startActivity(new Intent(Intent.ACTION_VIEW,
                         Uri.parse("http://ww.android.com")));
                break;
            case 1: //opens phone dialer and fills in the given number
                {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("content://contacts/people/")));
                break;}
            case 2:            
                {
                  startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("tel:12125551212")));
                 break;}
            case 3: //
                {
                Intent intent= new Intent(Intent.ACTION_WEB_SEARCH);
                intent.putExtra(SearchManager.QUERY, searchTerms);
                startActivity(intent);
                break;}
            case 4: // 
                {startActivity(new
                                Intent(Intent.ACTION_VOICE_COMMAND));
                break;}
                default: break;
            }
        }
    });
}
}

I'm new at Java and Android and I was wondering if it is possible to use an Intent in the case to bring up a new background followed by text for what item the user clicked (for example; they click open contacts and I tell them what open contacts is) or is there another way?

public class ListActivityExample extends ListActivity{
static final String[] ACTIVITY_CHOICES = new String[] {
    "Open Website Example",
    "Open Contacts",
    "Open Phone Dialer Example",
    "Search Google Example",
    "Start Voice Command"
};
final String searchTerms = "superman";

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, ACTIVITY_CHOICES));
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    getListView().setTextFilterEnabled(true);
    getListView().setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3){
            switch(arg2) {
            case 0: //opens web browser and navigates to given website
                 startActivity(new Intent(Intent.ACTION_VIEW,
                         Uri.parse("http://ww.android.com")));
                break;
            case 1: //opens phone dialer and fills in the given number
                {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("content://contacts/people/")));
                break;}
            case 2:            
                {
                  startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("tel:12125551212")));
                 break;}
            case 3: //
                {
                Intent intent= new Intent(Intent.ACTION_WEB_SEARCH);
                intent.putExtra(SearchManager.QUERY, searchTerms);
                startActivity(intent);
                break;}
            case 4: // 
                {startActivity(new
                                Intent(Intent.ACTION_VOICE_COMMAND));
                break;}
                default: break;
            }
        }
    });
}
}

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

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

发布评论

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

评论(3

德意的啸 2024-10-24 11:56:03

我注意到你错过了休息时间; case 2 中的语句意味着 case 2 将尝试执行 case 2 和 case 3

I noticed you are missing a break; statement in case 2 which would mean that case 2 would try to execute case 2 and case 3

颜漓半夏 2024-10-24 11:56:03

关于 switch 语句的两件事。

首先,您错过了 case 2 之后的中断。

其次,作为一个出色的编程标准,您应该在实例化其中的新变量时将 case 用括号括起来。

case 1:
{
  int nice = 0;
  ...
} break;

Two things about your switch statement.

First, you are missing the break after case 2.

Second, as a great programing standard you should enclose your cases with brackets when instantiating new variables within them.

case 1:
{
  int nice = 0;
  ...
} break;
探春 2024-10-24 11:56:03

我认为你需要定义一个ListView。还要在布局中添加一个列表视图。我分享了一个工作参考代码:

    public class MainMenu extends ListActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            try {
            String[] opt = getResources().getStringArray(R.array.MainMenu);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mainmenu);

            ListView lv = getListView();
            ListAdapter la = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, opt);
            lv.setAdapter(la);
            lv.setTextFilterEnabled(true);
            lv.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                    switch (position) {
                    case 0:
                        Intent firstIntent = new Intent(MainMenu.this,
                                After1.class);
                        startActivity(firstIntent);
                        break;
                    case 1:
                        Intent secondIntent = new Intent(MainMenu.this,
                                After2.class);
                        startActivity(secondIntent);
                        break;

                    default:
                        break;
                    }

                }

                @SuppressWarnings("unused")
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                }
            });

        } catch (Exception e) {
        }

    } // END onCreate()
}// END CLASS

I think you need to define a ListView. Also add a Listview in your Layout. I have shared a working reference code :

    public class MainMenu extends ListActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            try {
            String[] opt = getResources().getStringArray(R.array.MainMenu);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mainmenu);

            ListView lv = getListView();
            ListAdapter la = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, opt);
            lv.setAdapter(la);
            lv.setTextFilterEnabled(true);
            lv.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                    switch (position) {
                    case 0:
                        Intent firstIntent = new Intent(MainMenu.this,
                                After1.class);
                        startActivity(firstIntent);
                        break;
                    case 1:
                        Intent secondIntent = new Intent(MainMenu.this,
                                After2.class);
                        startActivity(secondIntent);
                        break;

                    default:
                        break;
                    }

                }

                @SuppressWarnings("unused")
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                }
            });

        } catch (Exception e) {
        }

    } // END onCreate()
}// END CLASS
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文