2013-06-23 42 views
1

我想發送一個字符串[]到另一個類的內部類中的線程(如果這是有道理的)。 然後,我想用String []做一些工作,然後將其輸出回UI。但我不知道如何做到這一點?我也是如何使用消息,以便我可以控制在UI中將要執行的操作。Android中的處理程序困惑

繼承人我MainActivity

public class MainActivity extends Activity implements OnClickListener { 
EditText cl; 
TextView info; 
Button enter; 
Button line; 
Button arc; 
Line callLine = new DrawingUtils.Line(); 
Enter callEnter = new DrawingUtils.Enter(); 
Arc callArc = new DrawingUtils.Arc(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    info = (TextView) findViewById(R.id.info); 
    enter = (Button) findViewById(R.id.enter); 
    line = (Button) findViewById(R.id.line); 
    arc = (Button) findViewById(R.id.arc); 
    cl = (EditText) findViewById(R.id.editText1); 

    Handler uiHandler = new Handler() { 
     public void handleMessage(Message msg) { 
      switch (msg.what) { 

      } 
     } 
    }; 

} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.enter: 
     String in = cl.getText().toString(); 
     String[] Input = in.split(","); 
     // I would like to send Input[] to the line Thread in DrawingUtils 
     callEnter.start(); 
     break; 
    case R.id.line: 
     callLine.start(); 
     break; 
    case R.id.arc: 
     callArc.start(); 
     break; 

    } 

}; 

} 

繼承人具有在課堂上與Thread其他類:

public class DrawingUtils { 

// Thread classes for buttons 
public static class Line extends Thread { 
    Thread line = new Thread() { 
     public void run() { 
      Looper.prepare(); 
      Handler lineHandler = new Handler() { 
       public void handleMessage(Message msg) { 
        // How to get Input from Enter button to use in thread 
       } 
      }; 
      Looper.loop(); 
      // Then I need to do some work 
      // Then Send the worked data back to the uiHandler in 
      // oncreate(). 
     } 
    }; 
} 

我使用處理器cuase他們似乎這就是你會爲我的代碼工作。當有人點擊Line時,它會設置一個textview(INPUT POINT1),然後線程將等待,當用戶輸入x,y,z到edittext並單擊Enter時,輸入將被放入一個字符串中,然後用逗號分隔並放入轉換成一個字符串數組,該數組將處理線程線程,然後在輸入代碼的末尾調用notifyAll()以允許線程繼續並請求下一個輸入。在行尾的線程將被處理回UI線程

+0

如果你使用的OpenGL,請告訴我們!我認爲AsyncTask可以正常工作,但熟練使用opengl的人可以給你特定的建議! –

+0

是的,生病了會使用opengl輸入將是x,y,z座標例如,當您點擊線時,它會要求點1,然後點2,然後繪製線。 –

回答

3

爲什麼要使用處理程序?我會用參數AsyncTask,它們在大多數情況下都很完美,就像你的。查看:http://developer.android.com/reference/android/os/AsyncTask.html

我會嘗試這個(MyAsyncTask是你的Activity類的子類):

private class MyAsyncTask extends AsyncTask<String[], Void, Boolean> { 
    //declare here local variables 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     //prepare your local variables for the computation 
    } 

@Override 
protected Boolean doInBackground(String[]... arg0) { 

    String[] myStringArray = arg0[0]; 
    // make your manipulation of myStringArray 

    return null; // return the result and set your local variable 
} 

    @Override 
    protected void onPostExecute(Boolean result) { 
     super.onPostExecute(result); 

     //update ui using result and/or local variable 
    } 
} 

從Click事件調用是這樣的:

String[] strings = {"1", "2", "3"}; 
new MyAsyncTask().execute(strings); 

我想以提醒您您的代碼:

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.enter: 
     String in = cl.getText().toString(); 
     String[] Input = in.split(","); 
     // I would like to send Input[] to the line Thread in DrawingUtils 
     callEnter.start(); 
     break; 
    case R.id.line: 
     callLine.start(); 
     break; 
    case R.id.arc: 
     callArc.start(); 
     break; 
    } 
}; 

變量Input僅在第一種情況下初始化,如果case語句選擇R.id.lineR.id.arc你有麻煩了......

+0

啊,你說的是初始化變量Input總是通過點擊第一個按鈕來完成的,ok。無論如何,當你完成從按鈕的點擊收集數據時,只需調用新的MyAsyncTask()。execute(字符串);它會進行計算並更新UI。不知道如果opengl繪圖有async任務的問題,但嘗試一下... –

+0

好吧,我會嘗試它,看看即時通訊不使用opengl截至目前爲止,我只想通過重新調整數組索引到textview來顯示我說它的工作。當我可以做到這一點,然後生病實施opengl。 –

+1

不要忘記,AsyncTask無法更新其'doInBackground()'中的UI元素。使用'onPostExecute()'或'onProgressChanged()' – Rob013