我使用了一個可擴展的視圖,其中parent是文本視圖,而children是edittext視圖。我想爲不同的編輯文本顯示不同的提示。我試圖通過編程來完成,但沒有成功。這是我的代碼。如何以編程方式在editext中顯示不同的提示
ContactFragment.java
public class ContactFragment extends Fragment {
private View mView;
Context mContext;
EditText edittext;
ExpandableListAdapter listAdapter;
ExpandableListView elv;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_contact_details, container, false);
ExpandableListView elv = (ExpandableListView) mView.findViewById(R.id.list);
prepareListData();
listAdapter = new ExpandableListAdapter(mContext, listDataHeader,listDataChild);
elv.setAdapter(listAdapter);
elv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
elv.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(mContext,
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
elv.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(mContext,
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
mContext,
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
return mView;
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Contact Details");
// Adding child data
List<String> basic = new ArrayList<String>();
basic.add(" ");
basic.add(" ");
basic.add(" ");
basic.add(" ");
basic.add(" ");
listDataChild.put(listDataHeader.get(0), basic); // Header, Child data
}
}
ExpandableListAdapter.java
public class ExpandableListAdapter extends BaseExpandableListAdapter implements TextWatcher{
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
private ArrayList<EditText> editTextList = new ArrayList<EditText>();
int i=1;
String name,purpose;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.fragment_contact_details_child, null);
}
EditText editetext = (EditText) convertView .findViewById(R.id.item);
while(i<6)
{
switch (i)
{
case 1:editetext.setHint("Contact Person");
break;
case 2:editetext.setHint("Contact Person Photo");
break;
case 3:editetext.setHint("Designation/Job");
break;
case 4:editetext.setHint("CEO Name");
break;
case 5:editetext.setHint("Company Name");
break;
}
i++;
}
editetext.addTextChangedListener(this);
editTextList.add(editetext);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.fragment_contact_details_parent, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.textViewParent);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
name=editTextList.get(0).getText().toString();
purpose=editTextList.get(1).getText().toString();
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
System.out.println("Name is"+name);
System.out.println("purpose is"+purpose);
}
}
首先,請張貼只有相關的代碼片段,而不是整個項目代碼。其次,「沒有成功」是什麼意思? EditText有setHint()方法,如果它不適合你 - 那麼解釋究竟出了什麼問題。 – Egor
確定我會記住這一點。是的,我嘗試使用你可以在我的代碼中看到。使用該結果完全不顯示提示 – Neha
while和嵌套switch語句的用途是什麼? – Pooya