2013-07-24 79 views
1

我有一個自定義對話框。其中一個editText id在那裏,我正在通過editText讀取輸入,並將輸入放到一個字符串中。此代碼後面跟着一個線程(子)來處理一個url。我想在提到的線程中使用這個字符串。但是在我輸入editText之前,線程正在被調用。我怎樣才能動態地使用線程內userinput相同的文本?在此先感謝..Android中的EditText值

公共無效的onClick(視圖v){

 switch (v.getId()) 
     { 
     case R.id.i1: 
      MyDevice.getInstance().currentUserImageId=R.drawable.jerry1; 
      MyDevice.getInstance().userName="You"; 
      MyDevice.getInstance().facebook=0; 


      this.finish(); 
      break; 



     case R.id.i2: 
      MyDevice.getInstance().currentUserImageId=R.drawable.chaplin1; 
      MyDevice.getInstance().userName="You"; 
      MyDevice.getInstance().facebook=0; 
      this.finish(); 
      break; 



     case R.id.i3: 
      MyDevice.getInstance().currentUserImageId=R.drawable.budy; 
      MyDevice.getInstance().userName="You"; 
      MyDevice.getInstance().facebook=0; 

      this.finish(); 
      break; 

     case R.id.facebook: 


      final Dialog dialog = new Dialog(this); 
      dialog.setContentView(R.layout.usernamefbdialog); 
      dialog.setTitle("Enter Facebook Username"); 


      Button dialogButton = (Button) dialog.findViewById(R.id.done); 
      // if button is clicked, close the custom dialog 
      dialogButton.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) 
       { 
        EditText edit=(EditText)dialog.findViewById(R.id.username); 
        text=edit.getText().toString(); 

         dialog.dismiss(); 

       } 
      }); 



      dialog.show(); 





      Thread thread = new Thread(new Runnable() 
      { 
       @Override 
       public void run() 
       { 
        try 
        {       

        MyDevice.getInstance().bitmap=getUserPic(text); 
        MyDevice.getInstance().facebook=1; 
        ImageView facebookImg=(ImageView) findViewById(R.id.facebookimg); 
        facebookImg.setImageBitmap(MyDevice.getInstance().bitmap); 

        } 

        catch (Exception e) 
        { 
         e.printStackTrace(); 
        } 
       } 
      }); 

      thread.start(); 


       break; 









     } 

} 
+3

發佈你的代碼,以便我們更容易理解你有什麼,以及你的問題在哪裏 – codeMagic

+0

保留參考創建的Thread並在需要時通過setter傳遞它的值。正如codeMagic所說,如果你發佈你的代碼,我們會更容易幫助。 – m0skit0

+0

使用您的文章下方的「編輯」按鈕在那裏添加您的代碼...更容易閱讀 – codeMagic

回答

1

基本上你想要做的就是從運行按鈕的Click事件中的線程,這樣它運行你的後文本。請參閱下面的修改代碼。 發生在字符串中的事件按此順序發生。 1.創建字符串 2.設置字符串等於編輯文本字符串 3.啓動線程,並使用String

 final String theStringYouWantToUseInTheThread = null;  
     Thread thread = new Thread(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       try 
       {       
       //use theStringYouWantToUseInTheThread here 
       MyDevice.getInstance().bitmap=getUserPic(text); 
       MyDevice.getInstance().facebook=1; 
       ImageView facebookImg=(ImageView) findViewById(R.id.facebookimg); 
       facebookImg.setImageBitmap(MyDevice.getInstance().bitmap); 

       } 

       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 
      } 
     }); 
     dialogButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) 
      { 
       EditText edit=(EditText)dialog.findViewById(R.id.username); 
       theStringYouWantToUseInTheThread = edit.getText().toString(); 
       thread.start(); 
       dialog.dismiss(); 
      } 
     }); 
     dialog.show(); 







     thread.start(); 
+0

感謝很多傢伙...工作非常快! – AshMv

0

我不能完全肯定,如果我的理解,但是這句話「哪有我動態地使用相同的文本從線程裏面userinput?「聽起來像是你要實現你的EditText

public class TempWatcher implements TextWatcher { 

    @Override 
    public void afterTextChanged(Editable s) { 
    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
    } 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) 
    { 
       // you can update something here 
    } 
} 

一個TextWatcher但是,如果你在你的EditText點擊Button,只有開始時檢查的有效值你的Thread那麼,它應該做你想做的。如果這不起作用,請澄清你的問題,並顯示你在哪裏開始Thread以及它是如何實施的

+0

感謝您的快速響應。當我將該線程放入點擊監聽器(在對話框內)時,正如ACR Development所建議的那樣,它工作正常。 – AshMv