Android 向其他活动发送数据并显示一些其他文本视图
我有一个Android应用程序,在其中我将数据从一个活动发送到另一个活动。我可以很容易地完成这一部分,但问题是我也想显示一些额外的文本视图。因此,当我使用Android 向其他活动发送数据并显示一些其他文本视图,android,android-intent,Android,Android Intent,我有一个Android应用程序,在其中我将数据从一个活动发送到另一个活动。我可以很容易地完成这一部分,但问题是我也想显示一些额外的文本视图。因此,当我使用setContentView显示发送的数据时,我只能看到发送的数据。我看不到其他文本视图。我希望它们同时显示 我搜索并试图找到一个解决方案,但没有任何运气。我附上代码供参考。谢谢 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInsta
setContentView
显示发送的数据时,我只能看到发送的数据。我看不到其他文本视图。我希望它们同时显示
我搜索并试图找到一个解决方案,但没有任何运气。我附上代码供参考。谢谢
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop_information);
Intent intent = getIntent();
String message = intent.getStringExtra(CropsListActivity.EXTRA_MESSAGE);
TextView textViewci= new TextView(this);
textViewci.setText(message);
TextView textView1= new TextView(this);
TextView textView2= new TextView(this);
textView1.setText("Major Districts");
textView2.setText("Suitable Soil");
当我使用此代码时,我看不到任何内容,活动仍然为空。实际上,我在这里使用的是
表格布局
,这样我可以以表格的形式显示数据您必须在布局上添加文本视图
View linearLayout = findViewById(R.id.info);
//LinearLayout layout = (LinearLayout) findViewById(R.id.info);
TextView valueTV = new TextView(this);
valueTV.setText("hallo hallo");
valueTV.setId(5);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
((LinearLayout) linearLayout).addView(valueTV);
您必须在parentview中添加文本视图。例如,如果在线性布局中使用了文本视图,则在线性布局中添加文本视图,然后使用添加视图(linearlayout) 下面是一个例子
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
tv1.setId(23);
TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);
您是否收到任何错误消息?如果是,请在这里张贴。其他人是对的。你需要将你的视图添加到父布局。我自己已经找到了答案。这相当简单,并且没有必要在父布局中使用视图。我修改了xml文件中的文本视图,并使用
findViewById
获得了它们的值。我正在附加更新的代码
I have just make a test program for you. first class code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, SecondClass.class);
intent.putExtra("firstValue", "1");
intent.putExtra("secondValue", "2");
startActivity(intent);
}
});
Second class code :
setContentView(R.layout.activity_crop_information);
Intent intent = getIntent();
String message1 = intent.getStringExtra("firstValue");
String message2 = intent.getStringExtra("secondValue");
RelativeLayout layout = new RelativeLayout(this);
TextView textView1= new TextView(this);
TextView textView2= new TextView(this);
TextView textViewci= new TextView(this);
textViewci.setText(message1+""+message2);
Log.e("intent value is", ""+message1+""+message2);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, textView1.getId());
textView1.setText("Major Districts");
textView2.setText("Suitable Soil");
layout.addView(textView1);
layout.addView(textView2);
addContentView(layout, lp);
Try and let me know. YOu have to make related layouts for both classes.
Hope this will help
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop_information);
Intent intent = getIntent();
String message = intent.getStringExtra(CropsListActivity.EXTRA_MESSAGE);
textView=(TextView)findViewById(R.id.textViewci);
textView1=(TextView)findViewById(R.id.textViewmd);
textView2=(TextView)findViewById(R.id.textViewso);
textView.setText(message);
textView1.setText("Major Districts");
textView2.setText("Suitable Soil");
tf = Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
textView.setTypeface(tf);
}
对不起,我没有得到你的答案。请描述一下好吗?为了更好地理解上面的示例,您必须在您的布局Akashshow链接中添加您的文本视图,但您也可以尝试线性布局LinearLayout LinearLayout=findViewById(R.id.info);我试过这样做,但我的输出没有变化,仍然是空白!是否要在文本视图中显示文本?是的,这是我的问题。您可以将文本视图添加到xml布局文件中。使用findViewById()方法在活动中访问它们,然后在其中显示文本。e、 g,
TextView textView1=(TextView)findViewById(R.id.textView1)//textView1在布局xml文件textView1.setText(“某些文本”)中声明代码>哈哈,我已经回答了我的问题,这与你发布之前的评论相同。谢谢你的帮助!没问题。很高兴看到你找到了解决办法。有没有特别需要使用这两个语句tf=Typeface.createFromAsset(getAssets(),“font/SHRUTIB.TTF”);textView.setTypeface(tf)代码>我已经得到了答案。感谢所有试图回答我问题的人。谢谢!