Android 3新特性+源码实现(4)


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


/**
 *
 * @author jankey
 *
 */
public class SearchViewDemo extends Activity implements OnQueryTextListener{


 private SearchView mSV;
 private ListView mList;
 
 private final String[] mStr = {"a","b","c","d","蒋","世","德","jankey"};
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  getWindow().requestFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.searchview);
  findViewById();
 }
 
 private void findViewById(){
  mList = (ListView) findViewById(R.id.list_view);
  mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStr));
  mList.setTextFilterEnabled(true);
  mSV = (SearchView) findViewById(R.id.search_view);
  mSV.setIconifiedByDefault(false);
  mSV.setOnQueryTextListener(this);
  mSV.setSubmitButtonEnabled(true);
  mSV.setQueryHint("Search!");
 }


 @Override
 public boolean onQueryTextChange(String newText) {
  if(TextUtils.isEmpty(newText)){
   mList.clearTextFilter();
  }else{
   mList.setFilterText(newText.toString());
  }
  return true;
 }


 @Override
 public boolean onQueryTextSubmit(String query) {
  Toast.makeText(this, "Search:"+query, 0).show();
  return false;package com.jsd.demo;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.StackView;


/**
 *
 * @author jankey
 *
 */
public class StackViewActivity extends Activity{


 private int color[] = {Color.BLUE,Color.CYAN,Color.GRAY,Color.GREEN,Color.WHITE};
 StackView stackView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.stack_view);
  findViewById();
 }
 
 private void findViewById(){
  stackView = (StackView) findViewById(R.id.stackView);
  ColorAdapter ca = new ColorAdapter(this, color);
  stackView.setAdapter(ca);
  final Button pre = (Button) findViewById(R.id.previer);
  pre.setOnClickListener(listener);
  final Button next = (Button) findViewById(R.id.next);
  next.setOnClickListener(listener);
 }
 
 private OnClickListener listener = new OnClickListener() {
  @Override
  public void onClick(View v) {
   switch (v.getId()) {
   case R.id.next:
    stackView.showNext();
    break;
   case R.id.previer:
    stackView.showPrevious();
    break;
   }
  }
 };
}
 }
}

<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item
     android:id="@+id/search"
     android:icon="@android:drawable/ic_menu_search"
     android:title="popu_search"
    />
    <item
     android:id="@+id/add"
     android:icon="@android:drawable/ic_menu_add"
     android:title="add"
     />
    <item
     android:id="@+id/edit"
     android:icon="@android:drawable/ic_menu_edit"
     android:title="edit"
     > 
     <menu>
      <item
       android:id="@+id/share"
       android:icon="@android:drawable/ic_menu_share"
       android:title="popu_share"
       /> 
     </menu>
     </item>
    <item
     android:id="@+id/edit"
     android:icon="@android:drawable/ic_menu_edit"
     android:title="popu_edit"
     /> 
</menu>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwgsws.html