如何在布局中的视图之间传输数据

时间:2011-04-17 16:55:38

标签: java android view

我无法找到以下问题的答案:

如何在同一LinearLayout中使用View A和View B从View A到View B获取数据?这甚至可能吗?我是否需要开始使用线程?

我猜不到正确的搜索短语,我可能不是第一个想要这样做的人,但我找不到它:(

以下是我现在用来创建视图的内容。在Tar​​getTrainer(扩展View)中,我让用户提供一些输入,我希望能够在TextView中向用户提供反馈。例如,我如何在TextView中显示TargetTrainer的onTouchEvent的坐标?

以下是我的程序的剪辑/简化版本。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    LinearLayout linear;
    linear = new LinearLayout(this);

    linear.setOrientation(LinearLayout.VERTICAL);
    TextView text = new TextView(this);
    text.setText("Test");
    linear.addView(text);

    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();
    int height = display.getHeight();

    TargetTrainer t = new TargetTrainer(this, width, height);
    linear.addView(t);
    setContentView(linear);

}

2 个答案:

答案 0 :(得分:1)

正如我从代码片段中看到的那样,您已经在构造函数new TargetTrainer(this, width, height)中传递了Context。假设您提供的代码来自名为BaseActivity的活动,请在BaseActivity构造函数中创建对TargetTrainer的引用,并从TargetTrainer调用更新方法。

public TargetTrainer extends View {

    ....
    BaseActivity mBaseActivity = null;


    public MyView(Context context, int width, int height) {
    ....
    mBaseActivity = (BaseACtivity)context;
    ....        
    }

    ....

    private void update(String text)
    {
        mBaseActivity.updateTextView(text);
    }
}

BaseActivity创建updateTextView

public void updateTextView(String updateText){ 
    text.setText(updateText);
}

答案 1 :(得分:0)

您应该设置该TextView的ID,收听TargetTrainer中的触摸事件,并在发生一次时使用

final TextView tv = (TextView)TargetTrainer.this.findViewById(R.id.myTextView);
tv.setText(touchEvent.toString());

就是这样。

<强>更新

如果你从xml源构建主要布局,它会更清晰 您需要在/ res / layout中创建一个新的布局xml,它看起来就像您在onCreate方法中创建的那个:

<强> RES /布局/ main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:id="@+id/myTextView" android:text="Test"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />
    <!-- change the your.package part to match the package declaration 
        of your TargetTrainer class -->
    <your.package.TargetTrainer android:id="@+id/myTargetTrainer"
        android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>

这样,一个新条目将被放置在R类的静态layout类中,名称为main
您可以通过R.layout.main引用它。

请注意,在此xml中,您为两个

定义了id属性
  • 您的TextView:myTextView
  • 您的TargetTrainer: 'myTargetTrainer'。

xml标记内的@ + id表示您正在使用“/”符号后面的名称创建新ID。
这也将在您的R类“静态id类中创建新成员,其中包含您提供的名称:myTextViewmyTargetTrainer,从现在开始可以从代码中的任何位置访问。

如果你已经构建了这个xml,那么onCreate方法将如下所示:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // attach the OnTouchListener to your TargetTrainer view:
    (TargetTrainer)findViewById(R.id.myTargetTrainer).setOnTouchListener(this);
}

您还必须扩展主活动类以实现View.OnTouchListener接口,并在课程结束时添加必要的方法:

@Override
public boolean onTouch(View view, MotionEvent event)
{
    //note, that here the view parameter is the view the touch event has been dispatched to
    final TextView tv = (TextView)findViewById(R.id.myTextView);
    tv.setText(event.toString());
    return true; //or false, if you are dealing further with this event in parent classes
}
相关问题