layout文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp2.TestActivity10" 11 android:orientation="vertical"> 12 13 <AutoCompleteTextView 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:hint="请输入" 17 android:id="@+id/at_1" 18 android:completionThreshold="1"/> 19 <LinearLayout 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content"> 22 <TextView 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:text="学历:"/> 26 <Spinner 27 android:layout_width="0dp" 28 android:layout_weight="1" 29 android:layout_height="wrap_content" 30 android:id="@+id/sp_1"></Spinner> 31 </LinearLayout> 32 <Button 33 android:layout_width="match_parent" 34 android:layout_height="wrap_content" 35 android:text="发送状态栏消息" 36 android:onClick="bt_onClick"/> 37 </LinearLayout>
java类:
1 package com.hanqi.testapp2; 2 3 import android.app.Notification; 4 import android.app.NotificationManager; 5 import android.app.PendingIntent; 6 import android.content.Intent; 7 import android.support.v7.app.AppCompatActivity; 8 import android.os.Bundle; 9 import android.view.View; 10 import android.widget.AdapterView; 11 import android.widget.ArrayAdapter; 12 import android.widget.AutoCompleteTextView; 13 import android.widget.Spinner; 14 import android.widget.Toast; 15 16 public class TestActivity10 extends AppCompatActivity { 17 18 AutoCompleteTextView at_1; 19 Spinner sp_1; 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_test10); 24 at_1 = (AutoCompleteTextView)findViewById(R.id.at_1); 25 sp_1 = (Spinner)findViewById(R.id.sp_1); 26 //准备数据源 27 String[] strings = {"abc","and","bea","car","AFcgy","ctret","Auyn"}; 28 //准备适配器 29 ArrayAdapter<String> arrayAdapter= new ArrayAdapter<String>(this,R.layout.arry_adapter,strings); 30 //给组件设置适配器 31 at_1.setAdapter(arrayAdapter); 32 33 //下拉列表 34 final String[] xl = {"高中","专科","本科","硕士","博士"}; 35 ArrayAdapter<String> sp = new ArrayAdapter<String>(this,R.layout.arry_adapter,xl); 36 sp_1.setAdapter(sp); 37 //监听 38 sp_1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 39 @Override 40 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 41 Toast.makeText(TestActivity10.this, "选中的项目是 "+xl[position], Toast.LENGTH_SHORT).show(); 42 } 43 44 @Override 45 public void onNothingSelected(AdapterView<?> parent) { 46 Toast.makeText(TestActivity10.this, "什么也没选", Toast.LENGTH_SHORT).show(); 47 } 48 }); 49 } 50 //发消息 51 public void bt_onClick(View v) 52 { 53 //1.得到状态栏消息管理器 54 NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 55 56 //准备PendingIntent 57 //1)创建Intent 58 Intent intent = new Intent(this,TestActivity1.class); 59 //2)生成 60 PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0); 61 //2.构建状态栏消息 62 Notification nt = new Notification.Builder(this) 63 .setContentTitle("天气预报") 64 .setContentText("明天晴,气温30°") 65 .setSmallIcon(R.drawable.gmail) 66 .setTicker("新的天气预报") 67 .setDefaults(Notification.DEFAULT_SOUND) 68 .setAutoCancel(false) 69 .setContentIntent(pi) 70 .build(); 71 //3.由管理器发送消息 72 nm.notify(0,nt); 73 } 74 }
效果图为:
点击状态通知栏会跳转到另一个Activity界面
附相关思维导图:
时间: 2024-10-10 11:32:13