.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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.hanqi.application3.UIActivity7" android:orientation="vertical"> <AutoCompleteTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="自动完成文本编辑框" android:id="@+id/actv_1"/> <Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/sp_1"> </Spinner> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="状态栏提示" android:onClick="notification_onClick"/> </LinearLayout>
.java
package com.hanqi.application3; import android.app.Notification; import android.app.NotificationManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Spinner; import android.widget.Toast; public class UIActivity7 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ui7); AutoCompleteTextView actv_1 = (AutoCompleteTextView)findViewById(R.id.actv_1); //设置自动完成的数据源 String[] strings = {"China","Child","Chile","Chinese","Add","All","Class","Ddp"}; ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.layout_array,strings); actv_1.setAdapter(arrayAdapter); //下拉列表 Spinner sp1= (Spinner)findViewById(R.id.sp_1); sp1.setAdapter(arrayAdapter); sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(UIActivity7.this, "position ="+position, Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { Toast.makeText(UIActivity7.this, "什么也没选", Toast.LENGTH_SHORT).show(); } }); } public void notification_onClick(View v) { //获取状态栏消息管理器 NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); // Notification nt = new Notification.Builder(this) .setContentTitle("这是一个通知") .setContentText("这是通知内容:明天休息 ") .setTicker("新通知") .setSmallIcon(R.drawable.da1) .setAutoCancel(true) .setDefaults(Notification.DEFAULT_SOUND) .build(); //交给管理器,发出消息 nm.notify(0,nt); } }
layout_array.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:textSize="30sp" android:gravity="center_vertical" android:paddingLeft="10dp"> </TextView>