private void textview(String question) {
TextView clientAddress = new TextView(this);
clientName.setGravity(1);
clientAddress.setTypeface(Typeface.defaultFromStyle(1));
clientAddress.setText(question);
clientAddress.setLayoutParams(lparams);
linearLayout.addView(clientAddress);
clientAddress.setTextSize(20);
clientAddress.setTextColor(getResources().getColor(R.color.white));
clientAddress.setPadding(7, 10, 0, 10);
}
我正在调用此函数来动态创建6个TextView。但我很困惑,如何从这些TextViews获取文本。谁能建议我解决方案?
答案 0 :(得分:0)
您可以使用gettext()方法从文本视图中获取文本。如果你想将获取的文本转换为字符串,那么你可以使用tostring()。
String fetchedtext = clientAddress.getText().toString();
答案 1 :(得分:0)
您必须为每个TextView
创建一个不同的对象。使用这些对象后,您可以获得TextView
的值。使用getText()
的{{1}}属性。
您还可以创建TextView
的数组。
答案 2 :(得分:0)
做这样的事情
private void textview(int position, String question) {
TextView clientAddress = new TextView(this);
clientName.setGravity(1);
clientAddress.setTypeface(Typeface.defaultFromStyle(1));
clientAddress.setText(question);
clientAddress.setTag(position);
clientAddress.setLayoutParams(lparams);
linearLayout.addView(clientAddress);
clientAddress.setTextSize(20);
clientAddress.setTextColor(getResources().getColor(R.color.white));
clientAddress.setPadding(7, 10, 0, 10);
}
public String getTextFromTextView(int positionOfTextView , LinearLayout mLinearLayout){
String result = "";
for (int i = 0; i <((ViewGroup)mLinearLayout).getChildCount() ; i++) {
if(((ViewGroup) mLinearLayout).getChildAt(i)!=null ){
View mView =((ViewGroup) mLinearLayout).getChildAt(i);
if(mView instanceof TextView){
TextView mTextView = (TextView)mView;
if(mTextView!=null && mTextView.getTag()!=null && positionOfTextView==(int) mTextView.getTag() ){
result =mTextView.getText().toString();
}
}else {
TextView mTextView = (TextView) mView.findViewWithTag(positionOfTextView);
if(mTextView!=null){
result =mTextView.getText().toString();
}
}
}
}
return result;
}