我实际上是Android编程的新手,我正在开发一个应用程序,其中有一个listView,它仅包含EditTexts(即TextInputEditText)。 我已经向EditTexts添加了提示,但是我想从用户输入的EditText中获取值。
这是我的代码:
MyItems.java
public class MyItem {
public String TextTitle;
EditText Message;
public MyItem(String title)
{
this.TextTitle = title;
}
public EditText getEditText() {
return Message;
}`enter code here`
public void setEditText(EditText msg) {
this.Message = msg;
}
}
MyAdapter.java
public class MyAdapter extends BaseAdapter {
private ArrayList<MyItem> listItems;
private Context context;
public MyAdapter(Context context, ArrayList<MyItem> listItems)
{
this.context=context;
this.listItems=listItems;
}
@Override
public int getCount()
{
return listItems.size();
}
@Override
public MyItem getItem(int position)
{
return listItems.get(position);
}
@Override
public long getItemId(int position)
{
return 0;
}
@Override
public View getView(final int position, View view, ViewGroup parent)
{
View row;
ListViewHolder listViewHolder;
if(view == null) {
LayoutInflater layoutInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.my_adapter, parent, false);
listViewHolder = new ListViewHolder();
listViewHolder.tvname = row.findViewById(R.id.tvname);
row.setTag(listViewHolder);
}
else
{
row = view;
listViewHolder = (ListViewHolder) row.getTag();
}
try {
MyItem myItem = getItem(position);
String text = myItem.TextTitle;
listViewHolder.tvname.setHint(text);
}
catch (NullPointerException exec)
{
Log.e(TAG, "Execption ");
}
EditText etText = (EditText)row.findViewById(R.id.etText);
listItems.get(position).setEditText(etText);
return row;
}
}
MainFragment
public class ExistingStudentFragment extends Fragment {
Button btnExisting, btnNew;
Button btnShow, btnSubmit;
ArrayList<MyItem> myitems=new ArrayList<>();
ArrayList<MyItem> myitems2=new ArrayList<>();
ArrayList<MyItem> myitems3=new ArrayList<>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.existing_student_fragment,
container, false);
getTextView();
getTextView2();
getTextView3();
ListView listView, listView2, listView3;
final MyAdapter myAdapter, myAdapter2, myAdapter3;
listView = (ListView)view.findViewById(R.id.list_items);
myAdapter = new MyAdapter(getContext(), myitems);
listView2 = (ListView)view.findViewById(R.id.list_items2);
myAdapter2 = new MyAdapter(getContext(), myitems2);
listView3 = (ListView)view.findViewById(R.id.list_items3);
myAdapter3 = new MyAdapter(getContext(), myitems3);
listView.setAdapter(myAdapter);
listView2.setAdapter(myAdapter2);
listView3.setAdapter(myAdapter3);
Spinner spinner_pass = (Spinner)view.findViewById(R.id.spin_passtype);
List<String> passtype = new ArrayList<String>();
passtype.add("Ist Class");
passtype.add("IInd Class");
ArrayAdapter<String> SpinnerAdapter = new ArrayAdapter<String>
(this.getActivity(), android.R.layout.simple_spinner_item, passtype);
SpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdownitem);
spinner_pass.setAdapter(SpinnerAdapter);
btnExisting = (Button)view.findViewById(R.id.btnExisting);
btnNew = (Button)view.findViewById(R.id.btnNew);
btnShow = (Button)view.findViewById(R.id.btnshow);
btnSubmit = (Button)view.findViewById(R.id.btnsubmit);
btnExisting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((UserConcessionActivity)getActivity()).setViewPager(0);
}
});
btnNew.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((UserConcessionActivity)getActivity()).setViewPager(1);
}
});
btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = myAdapter.getItem(0).getEditText().toString();
}
});
return view;
}
private void getTextView()
{
myitems.add(new MyItem("Admission Number:"));
myitems.add(new MyItem("Roll Number:"));
myitems.add(new MyItem("Name Of Student:"));
myitems.add(new MyItem("Stream:"));
}
private void getTextView2()
{
myitems2.add(new MyItem("Last Certificate Number(VCH No):"));
myitems2.add(new MyItem("Last Ticket from(Station):"));
myitems2.add(new MyItem("Last Ticket Number(Last 4 digits):"));
myitems2.add(new MyItem("Last Ticket Period(Monthly/Quaterly):"));
myitems2.add(new MyItem("Last Issued Date:"));
}
private void getTextView3()
{
myitems3.add(new MyItem("From Station:"));
myitems3.add(new MyItem("Pass Type(I or II):"));
myitems3.add(new MyItem("Ticket Period:"));
}
}
答案 0 :(得分:0)
TextInputEditText从
AppCompatEditText
扩展到,来自文档:
EditText
的特殊子类,专门用作以下子类: TextInputLayout。使用此类可让我们在输入法时在IME中显示提示 “提取”模式。
因此,您应该能够从TextInputEditText
获取类似以下代码的文本:
String retrievedText = textInputName.getText().toString();
别忘了在java-kotlin端定义Fragment
视图的前缀(在您的情况下为view
)
TextInputEditText textInputName = view.findViewById(R.id.edittext_edit_word_1);
并且也采用Xml布局。
还有一个不错的示例in here,您可以使用。