package
com.example.androidui2;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
android.os.Bundle;
import
android.app.Activity;
import
android.view.Menu;
import
android.view.View;
import
android.widget.AdapterView;
import
android.widget.GridView;
import
android.widget.SimpleAdapter;
import
android.widget.Toast;
import
android.widget.AdapterView.OnItemClickListener;
public
class MainActivity extends Activity
{
private
GridView mainMenuGridView;
private
int[] mainMenuImageRes = {R.drawable.circle,R.drawable.circle,R.drawable.circle,R.drawable.circle};
private
String[] mainMenuStrs = {"拨号","所有联系人","使用说明","退出"};
@Override
protected
void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainMenuGridView
= (GridView)findViewById(R.id.gridview_main_menu);
initView();
}
//
init views
private
void initView()
{
//
init main-menu
List< HashMap <String,
Object>> datas = new ArrayList< HashMap <String,Object>>();
int
length = mainMenuStrs.length;
for
(int i = 0; i < length ;
i++)
{
HashMap<String,
Object> map = new HashMap< String ,
Object>();
map.put("ItemImageView",
mainMenuImageRes[i]);
map.put("ItemTextView",
mainMenuStrs[i]);
datas.add(map);
}
SimpleAdapter
menuAdapter = new SimpleAdapter(
MainActivity.this,datas,
R.layout.main_menu_item,
new
String[]{"ItemImageView","ItemTextView"},
new
int[]{R.id.ItemImageView,R.id.ItemTextView} );
mainMenuGridView.setAdapter(menuAdapter);
mainMenuGridView.setOnItemClickListener(new
MainMenuItemOnClick());
}
//
Main Menu Item On Click Function
public
class MainMenuItemOnClick implements OnItemClickListener
{
/**
arg0 : The AdapterView where the click happened
*
arg1 : The view within the AdapterView that was clicked
*
arg2 : The position of the view in the adapter
*
arg3 : The row id of the item that was clicked
**/
public
void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long
arg3)
{
HashMap< String ,
Object> item = (HashMap< String ,
Object>)arg0.getItemAtPosition(arg2);
if
(item.get("ItemTextView").equals(mainMenuStrs[0]))
{
Toast.makeText(getApplicationContext(),
mainMenuStrs[0],
Toast.LENGTH_SHORT).show();
}
if
(item.get("ItemTextView").equals(mainMenuStrs[1]))
{
Toast.makeText(getApplicationContext(),
mainMenuStrs[1],
Toast.LENGTH_SHORT).show();
}
if
(item.get("ItemTextView").equals(mainMenuStrs[2]))
{
Toast.makeText(getApplicationContext(),
mainMenuStrs[2],
Toast.LENGTH_SHORT).show();
}
if
(item.get("ItemTextView").equals(mainMenuStrs[3]))
{
Toast.makeText(getApplicationContext(),
mainMenuStrs[3],
Toast.LENGTH_SHORT).show();
}
}
}
@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;
}
}
|