我正在创建一个移动应用程序,其中编辑文本不断添加到ListView中。它工作正常,直到列表视图到达屏幕大小以后编辑文本附加发生在模拟器(nexus 6)的顶部,而在移动editText没有附加到达屏幕大小。
任何人都可以帮忙解决这个问题吗?
Git hub链接: https://github.com/Sudan1993/Bull-Cows-Android
public class CustomAdapter extends BaseAdapter {
public ArrayList<String> myItems = new ArrayList();
public CustomAdapter() {
//System.out.println("inside constructor default ::::::::");
myItems.add("");
notifyDataSetChanged();
}
public int getCount() {
return myItems.size();
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = getActivity().getLayoutInflater();
convertView = inflater.inflate(R.layout.list_view,parent,false);
//get the go button id and disable it
goButton = (Button) convertView.findViewById(R.id.go_button);
goButton.setVisibility(View.GONE);
final EditText editText =
EditText)convertView.findViewById(R.id.editText);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
System.out.println("hasFocus ::: " + hasFocus+"");
}
});
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
goButton.setVisibility(View.GONE);
if(s.length()!=0) {
prevSeq = s.subSequence(0, s.length() - 1);
c = s.charAt(s.length()-1);
}
System.out.println("prev string ::: " + prevSeq );
System.out.println("entered string ::: " + s );
//eliminate duplicates
check = false;
if(prevSeq!=null) {
for (char m : prevSeq.toString().toCharArray()) {
if (m == c) {
editText.setError("Duplicate");
check = true;
}
}
}
if(s.length() == 4 && !check) {
goButton.setVisibility(View.VISIBLE);
}
}
});
//get the id of bulls and cows
final TextView bullText = (TextView) convertView.findViewById(R.id.bulls);
final TextView cowText = (TextView) convertView.findViewById(R.id.cows);
goButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
bulls = cows = 0;
// Perform action on click
String strEntered = editText.getText().toString();
System.out.println(strEntered);
System.out.println(ltrToFind);
//different position increment the value of cows
for (int i = 0; i < ltrToFind.length(); i++)
for (int j = 0; j < strEntered.length(); j++)
if (ltrToFind.charAt(i) == strEntered.charAt(j) && i != j)
cows++;
//same position increment the value of bulls
for (int i = 0; i < ltrToFind.length(); i++)
if (ltrToFind.charAt(i) == strEntered.charAt(i))
bulls++;
System.out.println("bull and cow values ::: " + bulls + "\n" + cows);
//pass the values to next fragment if both bull and cow are zero
//if (mListener != null) {
if(bulls == 0 && cows == 0)
mListener.onFragmentInteraction(strEntered);
//}
bullText.setText(bulls + "");
cowText.setText(cows + "");
//disabling everything and adding a new row
goButton.setEnabled(false);
editText.setEnabled(false);
bullText.setEnabled(false);
cowText.setEnabled(false);
myItems.add("");
notifyDataSetChanged();
}
});
}
return convertView;
}
public long getItemId(int position) {
return position;
}
}
Fragment_one.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_id">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Words"
android:id="@+id/textView2"
android:height="@dimen/abc_action_bar_stacked_max_height"
android:gravity="center_vertical|center_horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="88dp"
android:layout_marginStart="88dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Bulls"
android:id="@+id/textView4"
android:height="@dimen/abc_action_bar_stacked_max_height"
android:gravity="center_vertical|center_horizontal"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/textView5"
android:layout_toStartOf="@+id/textView5"
android:layout_marginRight="26dp"
android:layout_marginEnd="26dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Cows"
android:id="@+id/textView5"
android:height="@dimen/abc_action_bar_stacked_max_height"
android:gravity="center_vertical|center_horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="21dp"
android:layout_marginEnd="21dp" />
<ListView
android:id="@+id/customlist"
android:layout_width="fill_parent"
android:layout_below="@+id/textView5"
android:layout_height="fill_parent"
android:descendantFocusability="afterDescendants"
/>
</RelativeLayout>
List_item.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"
>
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:hint="Type"
android:ems="10"
android:id="@+id/editText"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_marginLeft="47dp"
android:layout_marginStart="47dp"
android:textSize="40dp"
android:textAlignment="center"
android:maxLength="4"
android:editable="true"
android:enabled="true"
android:focusableInTouchMode="true"
android:focusable="true"
android:layout_weight="1"
android:inputType="textFilter"/>
<Button
android:id="@+id/go_button"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="right|center_vertical"
android:src="@drawable/go_button"
android:background="@drawable/go_button"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp" />
<EditText
android:layout_width="40dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/bulls"
android:textAlignment="center"
android:layout_marginRight="13dp"
android:layout_marginEnd="13dp"
android:layout_alignTop="@+id/cows"
android:layout_toLeftOf="@+id/cows"
android:layout_toStartOf="@+id/cows" />
<EditText
android:layout_width="40dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/cows"
android:layout_marginRight="44dp"
android:layout_marginEnd="44dp"
android:layout_alignBottom="@+id/editText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>