Android setText()无法处理膨胀文本视图
Android setText()无法处理膨胀文本视图,android,Android,下面是我在这里测试的代码,当我膨胀任何特定的小部件时,我无法在它上真正执行setText。它覆盖文本并返回最后一个字符串。在这里的输出屏幕中,我可以看到从xml添加的五个文本视图,但我试图通过编程方式设置的文本没有发生。我只看到str[]中的一个文本,即str[5],它是数组中的最后一个文本。如果我能解释我的问题,请告诉我 public class TestInflate extends Activity { /** Called when the activity is first creat
下面是我在这里测试的代码,当我膨胀任何特定的小部件时,我无法在它上真正执行setText。它覆盖文本并返回最后一个字符串。在这里的输出屏幕中,我可以看到从xml添加的五个文本视图,但我试图通过编程方式设置的文本没有发生。我只看到str[]中的一个文本,即str[5],它是数组中的最后一个文本。如果我能解释我的问题,请告诉我
public class TestInflate extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View[] inflated = new View[5];
for(int i = 0; i<5; i++)
{
TableLayout myTableLayout = (TableLayout)findViewById(R.id.TableLayout);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflated[i] = inflater.inflate(R.layout.testbutton, myTableLayout);
TextView userName = (TextView)inflated[i].findViewById(R.id.myName);
userName.setText(str[i]);//here i should get name but not getting
}
}
String[] str = {"a","s","d","r","t"};
}
嗨,这次是测试。
试试这个
public class MainActivity extends Activity {
/** Called when the activity is first created. */
String[] str = {"a","s","d","r","t"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout myTableLayout = (TableLayout)findViewById(R.id.TableLayout);
LayoutInflatedView myClasses [] = new LayoutInflatedView[5];
for(int i = 0; i<5; i++)
{
myClasses[i] = new LayoutInflatedView(this, myTableLayout);
myTableLayout.addView(myClasses[i]);
myClasses[i].setUserName(str[i]);
}
}
}
检查此项,如果出现任何问题,请告知我们
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
container.addView(addView);
您也可以在for循环中尝试这一点。
仅供参考:如果处于循环中,则膨胀小部件上的setText()看起来像是写过头了。我如何克服这一点?如果我只做一次,它将工作,但如果它是在一个循环中,如上面一个,它将不会:(真的很难弄清楚你想要做什么。你将同一文本视图设置了5倍……这很正常,你只看到最后一个文本,不是吗?我想他想在TableLayout中添加5个TableRows?在这种情况下,我想你必须创建一个膨胀视图数组并调用setText();与视图的每个文本视图分开。即“视图视图=新视图”[5] ;视图[i]=充气机。充气(R.layout.testbutton,myTableLayout)'嗨,阿迪尔,正如我告诉你的,当我将testButton.xml膨胀到我的TableLayout中时,我正在将textview添加到布局中,所以当我设置setText时,为什么我要再次添加它?请原谅,我太简单了。我已经用视图编辑了我的问题,我的输出是什么样子。你想做什么…!!你想做一个列表、按钮还是什么…?你可以吗解释你的预期输出和预期输出的目标…!好的。我有一个字符串数组(这基本上是数据库条目)我想看看有多少个条目,以及在那里输入的对应字符串是什么。现在它是动态的,因为我不知道有多少条条目是可能的。我必须使用布局充气器。我添加了一个常量文本,即名称:在输出中添加,另一个是存储在数据库中的名称。我看到了相同的结果我的数据库编码中的问题为了测试这个问题,我写了这个小测试代码,在这里我也看到了同样的问题。这里的字符串[]str={“a”,“s”,“d”,“r”,“t”};只代表我的数据库。请告诉我是否可以解释。
My output is like this
Name : t //str[5]
Name :
Name :
Name :
Name :
public class MainActivity extends Activity {
/** Called when the activity is first created. */
String[] str = {"a","s","d","r","t"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout myTableLayout = (TableLayout)findViewById(R.id.TableLayout);
LayoutInflatedView myClasses [] = new LayoutInflatedView[5];
for(int i = 0; i<5; i++)
{
myClasses[i] = new LayoutInflatedView(this, myTableLayout);
myTableLayout.addView(myClasses[i]);
myClasses[i].setUserName(str[i]);
}
}
}
public class LayoutInflatedView extends LinearLayout
{
TextView text;
public LayoutInflatedView(Context context, ViewGroup table)
{
super(context);
LayoutInflater layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=layoutInflater.inflate(R.layout.test_button,this);
text = (TextView)findViewById(R.id.myName);
}
public void setUserName(String text)
{
this.text.setText(text);
}
}
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
container.addView(addView);