小编And*_*ech的帖子

SearchView在具有自定义适配器的ListView中

伙计们我需要一些帮助.我一直在尝试在我的项目中实现SearchView.但是我的项目包含一个列表视图,附带了一个自定义适配器.我搜索了很多站点,但都有使用arrayadapter实现的示例. .假设我有一个Employee类,它包含名称和年龄字段.如何使用他们的名称来过滤员工..

public class MainActivity extends Activity implements SearchView.OnQueryTextListener
{
SearchView mSearchView;
ListView mListView;
//String mStrings[]= {"abc","def","efg","ghi","ijk","apple","agxhsdgc","abbbcc"};
ArrayList<Employee> emp_list=null;
EmployeeAdapter emp_adapter=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSearchView=(SearchView) findViewById(R.id.searchView1);
        mListView=(ListView) findViewById(R.id.listView1);

        emp_list=new ArrayList<Employee>();
        emp_list.add(new Employee("ABC", 24));
        emp_list.add(new Employee("ACB", 24));
        emp_list.add(new Employee("BVF", 28));
        emp_list.add(new Employee("BRT", 28));
        emp_list.add(new Employee("ANM", 23));

        emp_adapter=new EmployeeAdapter(MainActivity.this, emp_list);
        mListView.setAdapter(emp_adapter);

//      mListView.setAdapter(new ArrayAdapter<String>(this,
//                android.R.layout.simple_list_item_1,
//                mStrings));
        mListView.setTextFilterEnabled(true);
        setupSearchView();


    }
     private void setupSearchView() 
     {
            mSearchView.setIconifiedByDefault(false);
            mSearchView.setOnQueryTextListener(this);
            mSearchView.setSubmitButtonEnabled(true); 
            mSearchView.setQueryHint("Search Here");
        }

        @Override
        public boolean onQueryTextChange(String newText) 
        { …
Run Code Online (Sandbox Code Playgroud)

android android-listview baseadapter searchview

24
推荐指数
1
解决办法
5万
查看次数

在Android中从Fragment设置标题inToolBar

我一直在使用AppCompatv7 lib中的最新工具栏.我在ToolBar ViewGroup中放置了一个textview我想从我的activity中的片段中为这个Textview设置一个标题.如果是自定义操作栏((ActionBarActivity)getActivity) .setcustomView(..)本来可以完成这项工作.但是由于使用了这个ToolBar,我无法使用它.我也在我的BaseActivity中实现了一个由所有Activities继承的方法.这个BaseActivity包含我初始化的方法左侧的滑动抽屉.我必须初始化活动中的initDrawerLayout()方法,否则抽屉不会被初始化.如果我在片段中初始化它给我所有空的结果,抽屉的切换按钮和也不是自定义标题设置.

这是我的initDrawer代码..

public void initDrawerLayout(String toolbar_text) {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerRelative = (RelativeLayout) findViewById(R.id.drawer_relative);
        if (mDrawerLayout != null) {
            findViewById(R.id.drawer_btn_a).setOnClickListener(this);
            findViewById(R.id.drawer_btn_b).setOnClickListener(this);
            findViewById(R.id.drawer_btn_c).setOnClickListener(this);
            findViewById(R.id.drawer_btn_d).setOnClickListener(this);
            findViewById(R.id.drawer_btn_e).setOnClickListener(this);
            findViewById(R.id.drawer_btn_f).setOnClickListener(this);
            findViewById(R.id.drawer_btn_g).setOnClickListener(this);
            findViewById(R.id.drawer_btn_h).setOnClickListener(this);
            findViewById(R.id.drawer_btn_i).setOnClickListener(this);
            findViewById(R.id.drawer_btn_j).setOnClickListener(this);
            findViewById(R.id.drawer_btn_k).setOnClickListener(this);
            findViewById(R.id.drawer_btn_l).setOnClickListener(this);
            findViewById(R.id.my_layout).setOnClickListener(this);





            Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
            toolbar.setBackground(getResources().getDrawable(R.drawable.icn_actionbar_background));
            TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
            mTitle.setText(toolbar_text);
            mTitle.setTypeface(Typeface.DEFAULT_BOLD);
            if (toolbar != null) {
                setSupportActionBar(toolbar);
                 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            }
            toolbar.setNavigationIcon(R.drawable.ic_drawer);

             mDrawerToggle = new ActionBarDrawerToggle(
                    this,  mDrawerLayout, toolbar,
                    R.string.drawer_open, R.string.drawer_close
                );
                mDrawerLayout.setDrawerListener(mDrawerToggle);


            toolbar.setNavigationOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) …
Run Code Online (Sandbox Code Playgroud)

android android-toolbar

20
推荐指数
7
解决办法
4万
查看次数

在Android中的SearchView自定义字体

我一直在尝试将自定义字体设置为android.support.v7.widget.SearchView查询提示和View.I中输入的文本.我尝试通过创建TypeFace对象动态地将字体从assests设置为searchView,但问题是"SearchView不包含一个setTypeface(Typeface tf)方法." 我确实尝试过自定义的SearchView类但找不到.

 private void setFont(String font1, String font2, String font3)
 {
        Typeface tf = null;

        tf = UiUtil.changeFont(MainActivity.this, font1);
        btn1.setTypeface(tf);

        tf = UiUtil.changeFont(MainActivity.this, font2);
        btn2.setTypeface(tf);

        tf = UiUtil.changeFont(MainActivity.this, font3);
        btn3.setTypeface(tf);

         tf = UiUtil.changeFont(MainActivity.this, font3);
        // no set typeface method..

    }
Run Code Online (Sandbox Code Playgroud)

fonts android searchview android-typeface

12
推荐指数
2
解决办法
5210
查看次数

Android中的InputTextLayout setError()方法无法正常工作

我设计了一个自定义的InputTextLayout,它包含一个自定义的Edittext.这个自定义的Edittext扩展了"AppCompatEdittext".我的MainActivity也扩展了AppCompatActivity.但是我仍然无法使用TextInputLayout显示错误消息.以下是各种文件的代码片段.

main.xml中

<android.support.design.widget.TextInputLayout
            android:id="@+id/last_name_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/first_name_layout"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:layout_marginTop="12dp"
            android:background="@drawable/grey_button_normal"
            android:hint="Last name"
            android:minHeight="48dp"
            android:paddingTop="3dp"
            android:theme="@style/TextLabel">

            <com.feebite.widgets.StyleableEditText
                android:id="@+id/lastname_edittext"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_gravity="center_vertical"
                android:background="@android:color/transparent"
                android:gravity="center_vertical"
                android:minHeight="48dp"
                android:nextFocusDown="@+id/email_edittext"
                android:paddingBottom="10dp"
                android:paddingLeft="15dp"
                android:singleLine="true"
                android:textColor="@color/darkish_grey"
                android:textColorHint="@color/darkish_grey"
                android:textCursorDrawable="@null"
                android:textSize="18sp" />

        </android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

MainActivity.java

mLastNameLayout = (TextInputLayout) findViewById(R.id.last_name_layout);
 mLastNameLayout.setHint("Last name");
mLastNameLayout.setErrorEnabled(true);
Run Code Online (Sandbox Code Playgroud)

styles.xml中的自定义主题

<style name="TextLabel" parent="TextAppearance.AppCompat">
        <!-- Hint color and label color in FALSE state -->
        <item name="android:textColorHint">@color/darkish_grey</item>
        <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
        <item name="colorAccent">@color/darkish_grey</item>
        <item name="colorControlNormal">@color/darkish_grey</item>
        <item name="colorControlActivated">@color/dark_pink</item>
    </style> …
Run Code Online (Sandbox Code Playgroud)

android android-textinputlayout

10
推荐指数
2
解决办法
2922
查看次数

检测在Android中关闭键盘事件

我有一个需要像iOS中的文本字段一样运行的edittext.当我点击它时,应该可以编辑,否则它应该被禁用.假设用户想要编辑某个值,当他按下后退按钮时,随着键盘关闭,我希望编辑文本被禁用以进行编辑.

我正在使用的是setCursorVisible(true)和setCursorVisible(false)方法.我确实尝试过使用keyCode事件,但他们没有帮助.这是我到目前为止尝试过的:

@Override
    public void onBackPressed() {
// Had the InputMethodManager service here..
        if(imm.isAcceptingText())
        {
           Toast.makeText(ProfileActivity.this,"working",Toast.LENGTH_SHORT).show();
        }
else {
            super.onBackPressed();
            Toast.makeText(ProfileActivity.this,"back pressed called",Toast.LENGTH_SHORT).show();
        }
    }
Run Code Online (Sandbox Code Playgroud)

还尝试重写keyDown事件.

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //replaces the default 'Back' button action
        if(keyCode==KeyEvent.KEYCODE_BACK)
        {

            Log.d("123","called,called");
            return  true;

        }
        return super.onKeyDown(keyCode, event);
    }
Run Code Online (Sandbox Code Playgroud)

android android-softkeyboard android-input-method android-edittext

5
推荐指数
1
解决办法
7202
查看次数