2016-11-21 45 views
-6

第一碼如何嘗試在if條件

try { 
     InputStream is = getAssets().open("read_asset.txt"); 
     int size = is.available(); 
     byte[] buffer = new byte[size]; 
     is.read(buffer); 
     is.close(); 
     String text = new String(buffer); 
     TextView tv = (TextView)findViewById(R.id.text); 
     tv.setText(text); 
    } catch (IOException e) { 
     throw new RuntimeException(e); 
    } 

,這第二個代碼

if(txtweb.getText().toString().equals("Google Plus")) { 
     setContentView(R.layout.google); 


    } else if(txtweb.getText().toString().equals("Twitter")){ 
     setContentView(R.layout.google); 
    } 

我想結合嘗試代碼放到if條件,請幫助

+1

剛把第二個代碼移到第一個代碼中有什麼問題? –

+0

你是做什麼的?你的觀點並不清楚。 – javadaskari

回答

-1

像這樣結合:

try { 
    if(txtweb.getText().toString().equals("Google Plus")) { 
      setContentView(R.layout.google); 
    InputStream is = getAssets().open("read_asset.txt"); 
      int size = is.available(); 
      byte[] buffer = new byte[size]; 
      is.read(buffer); 
      is.close(); 
      String text = new String(buffer); 
      TextView tv = (TextView)findViewById(R.id.text); 
      tv.setText(text); 
     } else if(txtweb.getText().toString().equals("Twitter")){ 
      setContentView(R.layout.google); 
     } 
    } catch (IOException e) { 
      throw new RuntimeException(e); 
     } 
+0

其工作兄弟,謝謝 –

+0

爲什麼這是downvoted? –

0

您可以通過是第一個代碼的try {}部分中的任何函數。您是否嘗試複製第二個代碼並將其放置在try {}中?這是你想要達到的目標嗎?