我正在设计一个Android应用程序,我想在EditText中插入一个字符后才显示列表视图。以下是我的代码......
activity_user.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_user"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="manikz.user_interface.UserActivity">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:drawableLeft="@android:drawable/ic_menu_search"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="Search"
android:inputType="textPersonName"
android:focusable="false"
android:focusableInTouchMode="false" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_below="@+id/textView2"
android:layout_marginTop="84dp"
android:text="bruce"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#ffffff"/>
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXTENDLESS"
android:textColor="#ffffff"
android:textSize="30dp"
android:layout_alignTop="@+id/textView4"
android:layout_toRightOf="@+id/textView12"
android:layout_toEndOf="@+id/textView12" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="iffy"
android:textSize="29dp"
android:textStyle="bold"
android:rotation="-90"
android:textColor="#ffffff"
android:layout_below="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="pock"
android:rotation="90"
android:textSize="24dp"
android:textStyle="bold"
android:textColor="#ffffff"
android:layout_below="@+id/textView7"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Polyandry"
android:textSize="25dp"
android:textStyle="bold"
android:layout_marginTop="18dp"
android:textColor="#ffffff"
android:layout_below="@+id/textView11"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="flabbergasted"
android:textSize="27dp"
android:textColor="#ffffff"
android:layout_above="@+id/textView3"
android:layout_toRightOf="@+id/textView3"
android:layout_toEndOf="@+id/textView3"
android:layout_marginBottom="12dp" />
<TextView
android:id="@+id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Magnanimous"
android:textColor="#ffffff"
android:textSize="40dp"
android:textStyle="bold"
android:layout_below="@+id/textView10"
android:layout_toRightOf="@+id/textView4"
android:layout_toEndOf="@+id/textView4" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="monas"
android:textSize="40dp"
android:textStyle="bold"
android:textColor="#ffffff"
android:layout_below="@+id/textView6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="early-blooming"
android:textSize="20dp"
android:textColor="#ffffff"
android:layout_below="@+id/textView8"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Altruist"
android:textSize="29dp"
android:textStyle="bold"
android:textColor="#ffffff"
android:layout_below="@+id/textView14"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="murmuring"
android:textSize="40dp"
android:textColor="#ffffff"
android:layout_alignTop="@+id/textView9"
android:layout_alignRight="@+id/textView15"
android:layout_alignEnd="@+id/textView15" />
<TextView
android:id="@+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alluring"
android:textSize="26dp"
android:rotation="-90"
android:textColor="#ffffff"
android:layout_toRightOf="@+id/textView7"
android:layout_toEndOf="@+id/textView7"
android:layout_alignBottom="@+id/textView7" />
<TextView
android:id="@+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Devastating"
android:textSize="40dp"
android:textStyle="bold"
android:textColor="#ffffff"
android:layout_below="@+id/textView12"
android:layout_toRightOf="@+id/textView8"
android:layout_toEndOf="@+id/textView8" />
<TextView
android:text="Scrumptious"
android:textColor="#ffffff"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView14"
android:layout_centerHorizontal="true"
android:id="@+id/textView17" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
android:text="cozy"
android:textSize="40dp"
android:rotation="90"
android:textColor="#ffffff"
android:layout_below="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RUCKUS"
android:textColor="#ffffff"
android:layout_alignTop="@+id/textView10"
android:layout_alignLeft="@+id/textView7"
android:layout_alignStart="@+id/textView7" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list_view"
android:layout_below="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/textView9" />
这是我的活动文件 SecondActivity.java
public class SecondActivity extends AppCompatActivity {
private ListView mSearchNFilterLv;
private EditText mSearchEdt;
private ArrayList<String> mStringList;
private ValueAdapter valueAdapter;
private TextWatcher mSearchTw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initUI();
initData();
valueAdapter = new ValueAdapter(mStringList, this);
mSearchNFilterLv.setAdapter(valueAdapter);
mSearchEdt.addTextChangedListener(mSearchTw);
}
private void initData() {
mStringList = new ArrayList<String>();
mStringList.add("one");
mStringList.add("two");
mStringList.add("three");
mStringList.add("four");
mStringList.add("five");
mStringList.add("six");
mStringList.add("seven");
mStringList.add("eight");
mStringList.add("nine");
mStringList.add("ten");
mStringList.add("eleven");
mStringList.add("twelve");
mStringList.add("thirteen");
mStringList.add("fourteen");
mSearchTw = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s != null && s.toString().trim().length() > 0) {
mSearchNFilterLv.setVisibility(View.VISIBLE);
textView1.setVisibility(View.GONE);
textView2.setVisibility(View.GONE);
textView3.setVisibility(View.GONE);
textView4.setVisibility(View.GONE);
textView5.setVisibility(View.GONE);
textView6.setVisibility(View.GONE);
textView7.setVisibility(View.GONE);
textView8.setVisibility(View.GONE);
textView9.setVisibility(View.GONE);
textView10.setVisibility(View.GONE);
textView11.setVisibility(View.GONE);
textView12.setVisibility(View.GONE);
textView13.setVisibility(View.GONE);
textView14.setVisibility(View.GONE);
textView15.setVisibility(View.GONE);
textView16.setVisibility(View.GONE);
textView17.setVisibility(View.GONE);
} else {
mSearchNFilterLv.setVisibility(View.GONE);
textView1.setVisibility(View.VISIBLE);
textView2.setVisibility(View.VISIBLE);
textView3.setVisibility(View.VISIBLE);
textView4.setVisibility(View.VISIBLE);
textView5.setVisibility(View.VISIBLE);
textView6.setVisibility(View.VISIBLE);
textView7.setVisibility(View.VISIBLE);
textView8.setVisibility(View.VISIBLE);
textView9.setVisibility(View.VISIBLE);
textView10.setVisibility(View.VISIBLE);
textView11.setVisibility(View.VISIBLE);
textView12.setVisibility(View.VISIBLE);
textView13.setVisibility(View.VISIBLE);
textView14.setVisibility(View.VISIBLE);
textView15.setVisibility(View.VISIBLE);
textView16.setVisibility(View.VISIBLE);
textView17.setVisibility(View.VISIBLE);
}
valueAdapter.getFilter().filter(s);
}
}
};
}
private void initUI() {
mSearchNFilterLv = (ListView) findViewById(R.id.list_view);
mSearchEdt = (EditText) findViewById(R.id.editTexts);
mSearchNFilterLv.setVisibility(View.GONE);
textView1 = (TextView)findViewById(R.id.textView);
textView2 = (TextView)findViewById(R.id.textView2);
textView3 = (TextView)findViewById(R.id.textView3);
textView4 = (TextView)findViewById(R.id.textView4);
textView5 = (TextView)findViewById(R.id.textView5);
textView6 = (TextView)findViewById(R.id.textView6);
textView7 = (TextView)findViewById(R.id.textView7);
textView8 = (TextView)findViewById(R.id.textView8);
textView9 = (TextView)findViewById(R.id.textView9);
textView10 = (TextView)findViewById(R.id.textView10);
textView11 = (TextView)findViewById(R.id.textView11);
textView12 = (TextView)findViewById(R.id.textView12);
textView13 = (TextView)findViewById(R.id.textView13);
textView14 = (TextView)findViewById(R.id.textView14);
textView15 = (TextView)findViewById(R.id.textView15);
textView16 = (TextView)findViewById(R.id.textView16);
textView1 = (TextView)findViewById(R.id.textView17);
}
}
我希望在EditText中输入字符串时,只有列表视图可见,所有TextView都会被隐藏。欢迎任何帮助。
答案 0 :(得分:0)
您的代码的问题在于您在更改文本之前检查edittext,在没有任何内容之前,所以请在更改文本后检查edittext,这是一个示例代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="your text view"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:id="@+id/text_ap"/>
<ListView
android:id="@+id/listLikeWhatsapp"
android:layout_width="match_parent"
android:layout_above="@id/text_ap"
android:layout_height="match_parent"/>
</RelativeLayout>
答案 1 :(得分:0)
试试这个
mSearchTw = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s != null && s.toString().trim().length() > 0) {
mSearchNFilterLv.setVisibility(View.VISIBLE);
//Hide the text here
} else {
mSearchNFilterLv.setVisibility(View.GONE);
//Show the text here
}
valueAdapter.getFilter().filter(s);
}
};