XML文件:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:stretchColumns="0,3"
>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="24sp" />
<EditText
android:id="@+id/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:minWidth="200sp"
>
<requestFocus />
</EditText>
<TextView />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码"
android:textSize="24sp"/>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:minWidth="200sp"
android:inputType="textPassword" />
<TextView />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="进去" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="出来" />
<TextView />
</TableRow>
</TableLayout>
Activity_main.java文件代码:
final int NOTIFYID_1 = 123;
private String user = "rosys";
private NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText etUser = (EditText)findViewById(R.id.user);
if(!"".equals(etUser.getText())){
user = etUser.getText().toString();
}
sendNotification();
}
});
Button button2=(Button)findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
notificationManager.cancel(NOTIFYID_1);
// TODO Auto-generated method stub
// 让布局第一行显示
((TableRow)findViewById(R.id.tableRow1)).setVisibility(View.VISIBLE);
// 让布局第二行显示
((TableRow)findViewById(R.id.tableRow2)).setVisibility(View.VISIBLE);
((Button)findViewById(R.id.button1)).setText("登陆");
}
});
}
protected void sendNotification() {
// TODO Auto-generated method stub
Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.advise);
builder.setTitle("我的登陆状态是");
final int[]imageId = new int[]{R.drawable.img1,R.drawable.img2,
R.drawable.img3,R.drawable.img4};
final String[] title = new String[]{"伤心","happy","sad","smile"};
// 创建一个list集合
List<Map<String,Object>>listItems = new ArrayList<Map<String,Object>>();
// 通过for循环将突破ID和列表项文字放到map中,并添加到List集合中
for(int i = 0;i<imageId.length;i++){
// 实例话map对象
Map<String, Object>map = new HashMap<String, Object>();
map.put("image", imageId[i]);
map.put("title", title[i]);
listItems.add(map);
}
final SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,listItems,R.layout.items,
new String[]{"title","image"},new int[]{R.id.title,R.id.image});
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Notification notify = new Notification();
notify.icon = imageId[which];
notify.tickerText=title[which];
notify.when = System.currentTimeMillis();
notify.defaults = Notification.DEFAULT_SOUND;
notify.setLatestEventInfo(MainActivity.this, user, title[which], null);
notificationManager.notify(NOTIFYID_1,notify);
// 让布局第一行不显示
((TableRow)findViewById(R.id.tableRow1)).setVisibility(View.INVISIBLE);
// 让布局第二行不显示
((TableRow)findViewById(R.id.tableRow2)).setVisibility(View.INVISIBLE);
((Button)findViewById(R.id.button1)).setText("更改登录状态");
}
});
builder.create().show();
}
效果如图: