gpt4 book ai didi

Android - 如何实现搜索功能并将结果返回到 ListView 中?

转载 作者:行者123 更新时间:2023-11-29 01:30:55 25 4
gpt4 key购买 nike

我有一个 EditText,用户可以从字符串数组(arr [] = “A”、“B”、“C”)。我有一个 ListView,当用户单击键盘上的“完成”按钮时,我希望在其中显示结果?如何在我的代码中执行此操作?

截图如下: enter image description here

这是我的代码?

package com.mobilecomp.add.ssproj1;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}



package com.mobilecomp.add.ssproj1;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends Fragment {

public MainActivityFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
}

activity_main.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
android:name="com.mobilecomp.add.ssproj1.MainActivityFragment"
tools:layout="@layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />

fragment _main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivityFragment">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/inputText"
android:layout_centerHorizontal="true"
android:inputType="text" />

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/searchResult"
android:layout_below="@+id/inputText"
android:layout_centerHorizontal="true" />

</RelativeLayout>

最佳答案

我认为最好的选择是使用 TextWatcher

editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

// TODO Auto-generated method stub
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

// TODO Auto-generated method stub
}

@Override
public void afterTextChanged(Editable s) {

// TODO Auto-generated method stub
}
});

关于Android - 如何实现搜索功能并将结果返回到 ListView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31233328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com