Android switch语句麻烦
我是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我注意到你错过了休息时间; 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
关于 switch 语句的两件事。
首先,您错过了 case 2 之后的中断。
其次,作为一个出色的编程标准,您应该在实例化其中的新变量时将 case 用括号括起来。
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.
我认为你需要定义一个ListView。还要在布局中添加一个列表视图。我分享了一个工作参考代码:
I think you need to define a ListView. Also add a Listview in your Layout. I have shared a working reference code :