2

我只需要一些关于我正在制作的 android 程序的帮助。基本上,我的一个 XML 布局中有一个按钮,当按下该按钮时,它应该将该活动的名称添加到位于另一个 XML 布局中的 TextView 中。

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

 <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);
    }
}
4

1 回答 1

2

在课堂上,在ButtonAerobic_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);
于 2013-04-17T16:20:18.920 回答