Android开发ListActivity和隐式Intent

截图

代码详情:

import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ActivityExamples extends ListActivity {

    static final String[] ACTIVITY_CHOICES = new String[]{
            "打开网站",
            "打开联系人",
            "打开电话拨号",
            "打开浏览器搜索",
            "打开浏览器开始语音命令"
    };

    final String searchTerms = "superman";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_examples);

        //设置适配器
        setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, ACTIVITY_CHOICES));
        //设置选择器方式
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        //设置是否启用文本筛选
        getListView().setTextFilterEnabled(true);
        //点击事件
        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://developer.android.google.cn/")));
                        break;
                    case 1:
                        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:
                }
            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/juer2017/article/details/117751407
今日推荐