Android UI详解之Toast、Notification、SearchView

                                   Android UI详解之Toast、Notification、SearchView

一、Toast是一种非常方便的提示消息框

    它具有两个特点:

           ①Toast提示的消息不会获得焦点

           ②Toast提示的消息过一段时间就会自动消失

    开发步骤:

         ①调用Toast的构造器或makeText()静态方法创建一个Toast对象。

            ②调用Toast的方法来设置该消息提示的对其方式、页面边距

            ③调用show()方法显示消息

  

下面看一个例子

<span style="font-size:14px;">package org.crazyit.ui;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


public class ToastTest extends Activity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Button simple = (Button) findViewById(R.id.simple);
		// 为按钮的单击事件绑定事件监听器
		simple.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View source)
			{
				// 创建一个Toast提示信息
				Toast toast = Toast.makeText(ToastTest.this
					, "简单的提示信息"
				// 设置该Toast提示信息的持续时间
						, Toast.LENGTH_SHORT);
				toast.show();
			}
		});
		Button bn = (Button) findViewById(R.id.bn);
		// 为按钮的单击事件绑定事件监听器
		bn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View source)
			{
				// 创建一个Toast提示信息
				Toast toast = new Toast(ToastTest.this);
				// 设置Toast的显示位置
				toast.setGravity(Gravity.CENTER, 0, 0);
				// 创建一个ImageView
				ImageView image = new ImageView(ToastTest.this);
				image.setImageResource(R.drawable.tools);
				// 创建一个LinearLayout容器
				LinearLayout ll = new LinearLayout(ToastTest.this);
				// 向LinearLayout中添加图片、原有的View
				ll.addView(image);
				// 创建一个ImageView
				TextView textView = new TextView(ToastTest.this);
				textView.setText("带图片的提示信");
				// 设置文本框内字体的大小和颜色
				textView.setTextSize(30);
				textView.setTextColor(Color.MAGENTA);
				ll.addView(textView);
				// 设置Toast显示自定义View				
				toast.setView(ll);
				// 设置Toast的显示时间
				toast.setDuration(Toast.LENGTH_LONG);
				toast.show();
			}
		});
	}
}</span>

xml ,就是为简单的两个按钮,这里就不给出

二、Nofication 

        nofication 是显示在状态栏的通知,位于手机屏幕的上方,哪里一般显示了手机当前的网络状态、电池状态、时间等.Nofication是一种全局的通知,他由NotificationManager服务管理

常用的方法

       ①setDefaults() 设置通知LED灯、音乐、振动等。

       ②setAutoCancel()    设置点击通知后,状态栏自动删除通知

       ③setContentTitle()       设置通知标题

       ④setContentText()          设置通知内容

       ⑤setSmalllcon()         为通知设置图标

       ⑥setLargeIcon          为通知设置大图标

        ⑦setTick()                  设置通知在状态栏提示的文本

        ⑧setContentIntent()          设置点击通知后启动的组件

开发步骤:

     ①get

package org.crazyit.ui;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;


public class NotificationTest extends Activity
{
	static final int NOTIFICATION_ID = 0x123;
	NotificationManager nm;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 获取系统的NotificationManager服务
		nm = (NotificationManager) 
			getSystemService(NOTIFICATION_SERVICE);
	}

	// 为发送通知的按钮的点击事件定义事件处理方法
	public void send(View source)
	{
		// 创建一个启动其他Activity的Intent
		Intent intent = new Intent(NotificationTest.this
			, OtherActivity.class);
		PendingIntent pi = PendingIntent.getActivity(
			NotificationTest.this, 0, intent, 0);
		Notification notify = new Notification.Builder(this)
			// 设置打开该通知,该通知自动消失
			.setAutoCancel(true)
			// 设置显示在状态栏的通知提示信息
			.setTicker("有新消息")
			// 设置通知的图标
			.setSmallIcon(R.drawable.notify)
			// 设置通知内容的标题
			.setContentTitle("一条新通知")
			// 设置通知内容
			.setContentText("恭喜你,您加薪了,工资增加20%!")
			// // 设置使用系统默认的声音、默认LED灯
			// .setDefaults(Notification.DEFAULT_SOUND
			// |Notification.DEFAULT_LIGHTS)
			// 设置通知的自定义声音
			.setSound(Uri.parse("android.resource://org.crazyit.ui/"
				+ R.raw.msg))
			.setWhen(System.currentTimeMillis())
			// 设改通知将要启动程序的Intent
			.setContentIntent(pi).build();
		// 发送通知
		nm.notify(NOTIFICATION_ID, notify);
	}

	// 为删除通知的按钮的点击事件定义事件处理方法
	public void del(View v)
	{
		// 取消通知
		nm.cancel(NOTIFICATION_ID);
	}
}
编写打开通知要启动的Activity


 *
 */
package org.crazyit.ui;

import android.app.Activity;
import android.os.Bundle;

public class OtherActivity extends Activity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		//设置该Activity显示的页面
		setContentView(R.layout.other);
	}
}

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:gravity="center_horizontal"
	>
<Button
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content" 
	android:text="发送Notification"
	android:onClick="send"
	/>
<Button
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content" 
	android:text="删除Notification"
	android:onClick="del"
	/>	
</LinearLayout>


三、SearchView 

package org.crazyit.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;
import android.app.Activity;

public class SearchViewTest extends Activity implements
		<strong><span style="color:#ff0000;">SearchView.OnQueryTextListener</span></strong>
{
	private SearchView sv;
	private ListView lv;
	// 自动完成的列表
	private final String[] mStrings = { "aaaaa", "bbbbbb", "cccccc" };

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		lv = (ListView) findViewById(R.id.lv);
		lv.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, mStrings));
		<strong><span style="color:#ff0000;">lv.setTextFilterEnabled(true);</span></strong>
		<span style="color:#ff0000;"><strong>sv = (SearchView) findViewById(R.id.sv);
		// 设置该SearchView默认是否自动缩小为图标
		sv.setIconifiedByDefault(false);
		// 为该SearchView组件设置事件监听器
		sv.setOnQueryTextListener(this);
		// 设置该SearchView显示搜索按钮
		sv.setSubmitButtonEnabled(true);
		// 设置该SearchView内默认显示的提示文本
		sv.setQueryHint("查找");</strong></span>
	}

	// 用户输入字符时激发该方法
	@Override
	public boolean <span style="color:#ff0000;"><strong>onQueryTextChange</strong></span>(String newText)
	{
		if (TextUtils.isEmpty(newText))
		{
			// 清除ListView的过滤
			lv.clearTextFilter();
		}
		else
		{
			// 使用用户输入的内容对ListView的列表项进行过滤
			lv.setFilterText(newText);
		}
		return true;
	}

	// 单击搜索按钮时激发该方法
	@Override
	public boolean <span style="color:#ff0000;"><strong>onQueryTextSubmit</strong></span>(String query)
	{
		// 实际应用中应该在该方法内执行实际查询
		// 此处仅使用Toast显示用户输入的查询内容
		Toast.makeText(this, "您的选择是:" + query
				, Toast.LENGTH_SHORT).show();
		return false;
	}
}

主要还是看监听器,这个监听器有两个方法,一个是onQueryTextChange(String newText),当用户输入时触发,onQueryTextChange(String newText)当用户点击搜索按钮时触发


猜你喜欢

转载自blog.csdn.net/ustory/article/details/42612515
今日推荐