2013-04-17 25 views
2

我只需要一点帮助,我正在制作的android程序。基本上,我在其中一个XML布局中有一个按钮,当按下该按钮时,它应该将该活动的名称添加到位于另一个XML布局中的TextView。Android:通过点击按钮在不同xml布局的文本视图中添加文本

所以这是需要按下的按钮。

<Button 
    android:id="@+id/add_to_schedule" 
    android:layout_width="150dp" 
    android:layout_height="30dp" 
    android:layout_below="@+id/widget44" 
    android:layout_centerHorizontal="true" 
    android:text="@string/add_to_schedule" 
    android:textColor="#ffffffff" 
    android:textSize="12sp" 
    android:typeface="serif" /> 

而这是TextView,它在不同的XML布局,我希望它显示的信息。

<TextView 
     android:id="@+id/txtview" 
     android:layout_width="300dp" 
     android:layout_height="200dp" 
     android:layout_alignLeft="@+id/textview" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="76dp" 
     android:text="@string/textview" /> 

这是Button将被按下的类。

public class Aerobic_Steps extends Activity{ 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.aerobic_steps); 

    } 
} 

这是TextView的将属于类。

public class Training_Schedule extends Activity{ 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.training_schedule); 
    } 
} 
+0

使用束值传递给另一个活动 – Senthil

+0

我该怎么做? – Labtec599

+0

什么时候你想显示文本到其他布局?点击后你想开始活动并显示文本?或者你想在已经开始的活动上显示文本 – stinepike

回答

2

Aerobic_Steps类,呼叫Training_Schedule活性按钮的OnClick现在

public class Aerobic_Steps extends Activity implements OnClickListener { 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.aerobic_steps); 
    Button btn = (Button) findViewById (R.id.buttonName); 
    btn.setOnClickListener(this); 

} 
} 


@Override 
public void onClick(View view) { 
    // TODO Auto-generated method stub 
    String className = Aerobic_Steps.this.getClass().getSimpleName(); 
      Intent i = new Intent(this, Training_Schedule.class); 
      i.putExtras("name",className); 
      startActivity(i); 

} 

Training_Schedule活性,使用以下代码中OnCreate()

//Get the bundle 
    Bundle bundle = getIntent().getExtras(); 

    //Extract the data… 
    String name = bundle.getString(“name”); 
    TextView tv = (TextView) findViewByID(R.id.yourTextView); 
    tv.setText(name); 
+0

嗨,感谢您的答案,但我不认为它的作品,我的应用程序部队关闭。我想我可能会做错了,如果你能准确地告诉我我需要放什么以及那会怎样。 – Labtec599

+0

检查更新的答案,如有任何错误,帖子堆栈跟踪和相关代码! –

+0

嗨感谢您的答复,aerobic_steps我得到这个错误:类型视图中的方法setOnClickListener(View.OnClickListener)不适用于参数(Aerobic_Steps) – Labtec599

相关问题