如何以编程方式在editext中显示不同的提示

时间:2016-04-14 20:01:03

标签: java android

我正在使用一个可扩展视图,其中parent是文本视图,children是edittext视图。我想为不同的编辑文本显示不同的提示。我尝试以编程方式进行,但根本没有成功。这是我的代码。

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);

}
}

2 个答案:

答案 0 :(得分:0)

实际上我有更好的解决方案为什么你不要简单地使用xml代码在你的编辑文本中添加提示。对你来说这很容易,并且以编程方式对它进行编码并不是一种经验。你得到它

答案 1 :(得分:0)

ExpandableListAdapter下的getChildView课程中,您似乎在告诉应用程序循环显示所有提示并完成“公司名称”

可能会改变

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++;
}

switch (childPosition)
{
   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;
}