开发者

How to transfer data between Views in a Layout

I haven't beeen able to find the answer to the following question:

How do I get data from View A to View B, with View A and View B in the same LinearLayout? Is this even possible? Do I need to start working with threads?

I haven't been able to get the correct search phrase I guess, I'm probably not the first person that wants to do this, but I can't find it :(

Below is what I use now to create the views. In the TargetTrainer (which extends View) I'm letting the user give some input, and I'd like to be able to give feedback to the user in the TextView. How would I for instance show the coordinates of the onTouchEvent of TargetTrainer in the TextView?

Below is a clipped/simplified version of my program.

public void onCreate(Bundle sav开发者_Python百科edInstanceState) {
    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);

}


As I can see from the snippet, you already pass Context in the constructor new TargetTrainer(this, width, height). Assuming that the code you provided is from activity called BaseActivity create reference to BaseActivity in the TargetTrainer constructor and call the update method from 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);
    }
}

In BaseActivity create updateTextView:

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


You should set the id of that TextView, listen to the touch events in your TargetTrainer, and when one occures, you use

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

That's it.

Update

It would be much cleaner, if you'd build your main layout from an xml source.
You need to create a new layout xml inside the /res/layout that looks like the one you're creating inside your onCreate method:

res/layout/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>

This way a new entry will be placed inside your R class' static layout class with the name main.
You can reference it by R.layout.main.

Note, that in this xml you have id attributes defined for both

  • your TextView: myTextView, and
  • your TargetTrainer: 'myTargetTrainer'.

The @+id inside the xml tag means that you are creating a new id with the name after the '/' sign.
This also will create new members inside your R class' static id class with the names you've provided: myTextView and myTargetTrainer that are accessible from now on from anywhere in your code.

If you've built this xml, your onCreate method will look like this:

/** 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);
}

You also have to extend your main activity class to implement the View.OnTouchListener interface, and add the necessary method at the end of your class:

@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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜