2011-04-17 67 views
3

我还没有beeen能够找到答案以下问题转移次数之间的数据:如何在布局

我如何从一个视图获取数据查看B,与视图A和视图B相同的LinearLayout?这甚至有可能吗?我是否需要开始使用线程?

我一直没能得到正确的搜索短语我想,我可能并不想这样做的第一人,但我不能找到它:(

下面是我现在用来创建视图,在TargetTrainer中(它扩展了视图),我让用户给出一些输入,我希望能够在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); 

} 
+0

我想我可能需要做一些与上下文也许,因为我们需要传递到一个视图,但是那是相当不清楚我也 – 2011-04-17 16:59:18

回答

0

你应该设置的TextView的id,听你的TargetTrainer触摸事件,并且在一个occures,您使用

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

就是这样。

更新

这将是干净多了,如果你从一个XML源代码构建主布局。
你需要创建/ RES /布局,看起来像你的onCreate方法内部创建一个内布置一新的xml:

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,你有两个

  • 你的TextView定义ID属性: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 
} 
+0

谢谢!虽然还没有为我工作。我可以通过分配一个数字来手动设置ID吗?我没有得到R.layout.textview部分。无论如何,如果我分配一个随机数(6564)到textview,并把 最终TextView电视=(TextView)TargetTrainer.this.findViewById(6564); 我在tv.setText(touchEvent.toString())得到一个nullpointerexception; (这不是rouchevent:tv.setText(“test”);也是失败) – 2011-04-17 17:26:32

+0

我会在几分钟内更新答案,并共享一个干净的R和布局使用示例。 – rekaszeru 2011-04-17 17:32:56

+0

Youri的回答已经解决了我的烦恼,谢谢! – 2011-04-17 17:33:45

1

,我可以从片段所见,你已经在构造new TargetTrainer(this, width, height)传递上下文。假设您提供的代码来自BaseActivity活动,请在TargetTrainer构造函数中创建对BaseActivity的引用,并从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); 
} 
+0

Yay,工作!我有一个预感,整个环境的东西是有益的东西,但不知道什么:)这是我第一次做一个用户界面,我习惯倾倒输入/输出到文件 - 大量的东西学习;) – 2011-04-17 17:32:33