基礎練習:ListActivity範例實作(一)
1.用Intent及startActivity函式來瀏覽網站,程式碼如下:
package com.example.listviewtest;
import android.net.Uri;
import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ListActivity {
private String[] mStrings = {
"南開科技大學", "南投縣電腦公會"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, mStrings));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
Uri uri;
Intent i;
super.onListItemClick(l, v, position, id);
switch(position){
case 0:
uri=Uri.parse("http://www.nkut.edu.tw");
i=new Intent(Intent.ACTION_VIEW,uri);
startActivity(i);
break;
case 1:
uri = Uri.parse("http://nca.nantou.com.tw/?ptype=info");
i = new Intent(Intent.ACTION_VIEW, uri);
startActivity(i);
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
2.執行結果
沒有留言:
張貼留言