2011-04-17 62 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