Android setText()不同的布局文件
每次有人单击不同的图片时,我都试图使用setText在同一版面中显示不同的文本。 所以所有的布局文件都保持不变,唯一需要更改的是该布局中的android:text 我用case语句创建了一个类,用于当有人按下图片,然后调用setText() 但是看起来setText甚至没有被调用。因为我可以看到在同一个case语句中调用的Log.v,但是文本没有改变 PictureInfo.javaAndroid setText()不同的布局文件,android,Android,每次有人单击不同的图片时,我都试图使用setText在同一版面中显示不同的文本。 所以所有的布局文件都保持不变,唯一需要更改的是该布局中的android:text 我用case语句创建了一个类,用于当有人按下图片,然后调用setText() 但是看起来setText甚至没有被调用。因为我可以看到在同一个case语句中调用的Log.v,但是文本没有改变 PictureInfo.java public class PictureInfo extends Activity implements OnC
public class PictureInfo extends Activity implements OnClickListener {
private static final String TAG = "Popup";
public TextView infoText;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
infoText = (TextView)inflater.inflate(R.layout.information, null);
View a1Button = findViewById(R.id.a1);
a1Button.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.a1:
Intent a = new Intent(this, Information.class);
startActivity(a);
Log.v(TAG, "Change setText");
infoText.setText(R.string.a2_text);
break;
}
}
}
information.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/information_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a1_text"
/>
将代码顺序更改为:
//First change text
Log.v(TAG, "Change setText");
infoText.setText(R.string.a2_text);
//Then call new activity
Intent a = new Intent(this, Information.class);
startActivity(a);
瞧 您似乎正在膨胀
版面
-R.layout.information
,但将其转换为文本视图
。我真的不知道你在那里干什么
我认为您希望目标活动
在其布局中使用源活动
提供给它的一些文本。为什么不将要在Intent
上显示为Extra
的文本的id
传递给用户
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.a1:
Intent a = new Intent(this, Information.class);
intent.putExtra("com.packagename.identifier", R.string.a2_text);
startActivity(a);
break;
}
}
然后在您的信息活动中:
public class Information extends Activity {
...
TextView myTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
...
myTextView = (TextView) findViewById(R.id.myTextViewId);
...
Bundle extras = getIntent().getExtras();
if (extras != null) {
int textId = extras.getInt("com.packagename.identifier");
infoText.setText(textId);
}
}
}
在更改文本之前,您正在打开一个
活动
,因此无法看到更改的文本。@dave.c no my Information.java调用Information.xml,my PictureInfo.java调用main.xml。这就是为什么我在调用main.xml的setContentView时创建了inflate来加载information.xml),这样我就可以编辑information.xml的xml,即使我实际上没有将其作为layout@TanmayMandal,这不起作用可能是更改文本和启动新活动之间的时间太短了。如果删除startActivity,文本是否会更改?如果有。。您需要在更改它和转到下一个活动之间引入一段时间。我想我可以使用inflate添加另一个布局文件,这样我就可以使用其他布局文件ID,即使布局文件没有使用setContentView()加载。我现在让你的解决方案起作用了!您所做的是将a2_文本的字符串值放入一个将被推送到information.class的意图中,然后在information.class中拉出该字符串?我对这件事还不太熟悉,只是想了解它的全部;)。谢谢你快速准确的回答!确切地您的另一种方法也可能有效,但我发现这一方法意味着每个活动都在更新自己的布局,因此更易于使用。是的,我认为使用不同的类可以很容易地进行维护。我的观点是,我在同一个布局中显示了几个具有不同信息的图像,这就做到了,现在我可以通过每个case语句来实现这一点,只需更改R.string