Android——AutoCompleteTextView、Spinner和消息提示

时间:2022-12-11 22:13:32

layout文件:

 <?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.testapp2.TestActivity10"
android:orientation="vertical"> <AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入"
android:id="@+id/at_1"
android:completionThreshold="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学历:"/>
<Spinner
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/sp_1"></Spinner>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送状态栏消息"
android:onClick="bt_onClick"/>
</LinearLayout>

java类:

 package com.hanqi.testapp2;

 import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
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 TestActivity10 extends AppCompatActivity { AutoCompleteTextView at_1;
Spinner sp_1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test10);
at_1 = (AutoCompleteTextView)findViewById(R.id.at_1);
sp_1 = (Spinner)findViewById(R.id.sp_1);
//准备数据源
String[] strings = {"abc","and","bea","car","AFcgy","ctret","Auyn"};
//准备适配器
ArrayAdapter<String> arrayAdapter= new ArrayAdapter<String>(this,R.layout.arry_adapter,strings);
//给组件设置适配器
at_1.setAdapter(arrayAdapter); //下拉列表
final String[] xl = {"高中","专科","本科","硕士","博士"};
ArrayAdapter<String> sp = new ArrayAdapter<String>(this,R.layout.arry_adapter,xl);
sp_1.setAdapter(sp);
//监听
sp_1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(TestActivity10.this, "选中的项目是 "+xl[position], Toast.LENGTH_SHORT).show();
} @Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(TestActivity10.this, "什么也没选", Toast.LENGTH_SHORT).show();
}
});
}
//发消息
public void bt_onClick(View v)
{
//1.得到状态栏消息管理器
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); //准备PendingIntent
//1)创建Intent
Intent intent = new Intent(this,TestActivity1.class);
//2)生成
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
//2.构建状态栏消息
Notification nt = new Notification.Builder(this)
.setContentTitle("天气预报")
.setContentText("明天晴,气温30°")
.setSmallIcon(R.drawable.gmail)
.setTicker("新的天气预报")
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(false)
.setContentIntent(pi)
.build();
//3.由管理器发送消息
nm.notify(0,nt);
}
}

效果图为:

Android——AutoCompleteTextView、Spinner和消息提示Android——AutoCompleteTextView、Spinner和消息提示Android——AutoCompleteTextView、Spinner和消息提示Android——AutoCompleteTextView、Spinner和消息提示Android——AutoCompleteTextView、Spinner和消息提示

点击状态通知栏会跳转到另一个Activity界面

Android——AutoCompleteTextView、Spinner和消息提示

附相关思维导图:

Android——AutoCompleteTextView、Spinner和消息提示