【问题标题】:trigger SearchView of xml layout and get the text触发xml布局的SearchView并获取文本
【发布时间】:2014-09-15 16:32:31
【问题描述】:

我不知道我是否可以这样做

我已将 Sherlock.widget.SearchView 添加到我的 xml 布局中

    <com.actionbarsherlock.widget.SearchView
        android:id="@+id/searchView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="45dp" >
    </com.actionbarsherlock.widget.SearchView>

然后我将 xml 布局 id 获取到我的活动中

    etSearch = (SearchView) findViewById(R.id.searchView1);
         etSearch.setSubmitButtonEnabled(true);

现在我想触发 searchview 并获取从 searcbar 输入的文本并将其传递给另一个活动。

【问题讨论】:

    标签: android actionbarsherlock searchview


    【解决方案1】:

    你必须实现OnQueryTextListener接口:

    //你的活动

    SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
            public boolean onQueryTextChange(String newText) {
                return true;
            }
    
            public boolean onQueryTextSubmit(String query) {
                // Called when the user submits the query
                Intent intent = new Intent(YourActivity.this, NextActivity.class);
                intent.putExtra("query", query);
                YourActivity.this.startActivity(intent);
                return true;
            }
     };
    
     searchView.setOnQueryTextListener(queryTextListener);
    

    //下一个活动

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String value = extras.getString("query");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多