package com.example.searchviewtest; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.SearchView; import android.widget.SearchView.OnQueryTextListener; import android.widget.Toast; public class MainActivity extends Activity implements OnQueryTextListener{ private SearchView searchView = null; private ListView listView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); searchView = (SearchView)findViewById(R.id.search); listView = (ListView)findViewById(R.id.list); /** * 为ListView添加adapter * */ final String[] data= {"中国","巴基斯坦","中间","米国","中部","只是测试"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data); listView.setAdapter(adapter); listView.setTextFilterEnabled(true); //使能文本过滤器 /** * 设置SearchView相关属性 * */ searchView.setIconifiedByDefault(false);//设置SearchView默认是否自动缩小为图标 searchView.setOnQueryTextListener(this);//为该SearchView组件设置事件监听器 searchView.setSubmitButtonEnabled(true);//设置该SearchView显示搜索按钮 searchView.setQueryHint("请输入关键字"); } /** * 动态显示搜索到的结果 * */ @Override public boolean onQueryTextChange(String newText) { if(TextUtils.isEmpty(newText)){ //若搜索框无输入则清除ListView的过滤 listView.clearTextFilter(); }else{ listView.setFilterText(newText);//使搜索框字符与列表数据进行匹配显示 } return true; } /** * 单击搜索按钮时激发该方法 * */ @Override public boolean onQueryTextSubmit(String query) { Toast.makeText(this, "您的选择是:"+query, Toast.LENGTH_SHORT).show(); return false; } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <SearchView android:id="@+id/search" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1"/> </LinearLayout>
package com.example.srcollviewtest; import java.io.IOException; import java.io.InputStream; import android.app.Activity; import android.content.res.AssetManager; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { private TextView mText = null; private String parseResult = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mText = (TextView)findViewById(R.id.text); AssetManager assetManager = getAssets(); //获取"资产"管理对象 try { InputStream inputStream = assetManager.open("info.txt"); //获取指定文件输入流 int length = inputStream.available(); //获取输入流总字节数 byte[] buffer = new byte[length]; //字节数组缓存 inputStream.read(buffer); //将输入流中length个字节读入到字节数组中 mText.setText(new String(buffer, "GB2312")); } catch (IOException e) { e.printStackTrace(); } } }
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <HorizontalScrollView android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </HorizontalScrollView> </ScrollView>
package com.example.notificationtest; import android.app.Activity; import android.app.Notification; import android.app.Notification.Builder; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.ComponentName; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { private NotificationManager notificationManager; private final static int NOTIFICATION_ID=0x123; //通知唯一标识符ID @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取系统的NotificationManager服务 notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } /** *发送Notification通知 */ public void sendNotification(View v){ Intent intent = new Intent(); intent.setClassName("com.tencent.mobileqq","com.tencent.mobileqq.activity.SplashActivity"); //启动QQ客户端 PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); /** * 创建通知并指定相关属性 * */ Notification notify = new Notification.Builder(this) .setAutoCancel(true) //设置打开该通知,该通知自动消失 .setTicker("有新消息") //设置显示在状态栏的通知提示信息 .setSmallIcon(R.drawable.ic_launcher) //设置通知的图标 .setContentText("恭喜发财!") //设置通知内容的标题 .setContentText("您有新的QQ红包,猛戳领取?") //设置通知内容 .setDefaults(Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE)//LED灯、振动设置为默认 .setSound(Uri.parse("android.resource://com.example.notificationtest/"+R.raw.ring))//设置自定义声音 .setWhen(System.currentTimeMillis()) .setContentIntent(pendingIntent) //设置点击通知事件(启动微信) .build(); //创建通知 /** * 发送通知 * */ notificationManager.notify(NOTIFICATION_ID, notify); } /** * 删除NOTIFICATION_ID标志的通知 */ public void cancelNotification(View v){ notificationManager.cancel(NOTIFICATION_ID); } }
<span style="font-family:Times New Roman;font-size:18px;"> <!-- 添加LED、振动权限 --> <uses-permission android:name="android.permission.FLASHLIGHT"/> <uses-permission android:name="android.permission.VIBRATE"/></span>
原文地址:http://blog.csdn.net/u012637501/article/details/46503609