activity_main.xml代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="87dp"
android:layout_marginTop="22dp"
android:text="Button" />
</RelativeLayout>
menu.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/TV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.03"
android:background="#ffff" />
<ListView
android:id="@+id/gong_listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.32"
android:background="#fff"
>
</ListView>
</LinearLayout>
mainActivity代码:
import android.app.Activity;
import android.app.AlertDialog;import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;
public class MainActivity extends Activity {
LayoutInflater factory;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
//生成ListView
ListView listview=new ListView(MainActivity.this);
String[] strs=new String[2];
strs[0]="充值";
strs[1]="查询充值记录";
ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,
R.layout.mune,R.id.TV,strs);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0://充值
factory = LayoutInflater.from(MainActivity.this);
final View topDia = factory.inflate(R.layout.top_up, null);
AlertDialog topDiaLog = new AlertDialog.Builder(MainActivity.this).setTitle("支付宝充值")
.setView(topDia)
.setPositiveButton("充值", new DialogInterface.OnClickListener() {
private ProgressDialog chongZhi;
@Override
public void onClick(DialogInterface dialog, int which) {
chongZhi = ProgressDialog.show(MainActivity.this,
"请等待","正在为您充值...",true);
new Thread() {
public void run() {
try {
sleep(3000);
} catch (Exception e) {
e.printStackTrace();
} finally {
chongZhi.dismiss();
}
}
}.start();
Toast.makeText(MainActivity.this, "充值成功...", 1).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
topDiaLog.show();
break;
case 1://查询充值记录
factory = LayoutInflater.from(MainActivity.this);
final View chaXunDia = factory.inflate(R.layout.li_shi_ding_dan, null);
AlertDialog chaXunDiaLog = new AlertDialog.Builder(MainActivity.this).setTitle("查询历史订单")
.setView(chaXunDia).create();
chaXunDiaLog.show();
break;
}
System.out.println(position);
}
});
PopupWindow window=new PopupWindow(MainActivity.this);
//设置宽高
window.setWidth(200);
window.setHeight(100);
//window.setBackgroundDrawable(d);
//填充
window.setContentView(listview);
//外部可部
window.setOutsideTouchable(true);
//加了下面这行,onItemClick才好用
window.setFocusable(true);
window.showAsDropDown(button1);
}});
}
}