2014-03-14 78 views
0

好吧,我有一個活動,用戶知道他/她的IP地址,IP顯示在TextView中,但我希望它顯示在一個EditView爲容易複製粘貼。這是我的代碼...Android:如何在EditView和TextView中顯示字符串數據

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_ip); 

    getWindow 
    ().setSoftInputMode(
     WindowManager. 
     LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
    try { 
     for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { 
      NetworkInterface intf = en.nextElement(); 
      for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { 
       InetAddress inetAddress = enumIpAddr.nextElement(); 
       if (!inetAddress.isLoopbackAddress()) { 
        TextView ipView= (TextView) findViewById(R.string.ip); 
        ipView.setText(inetAddress.getHostAddress()); 
       } 
      } 
     } 
    } catch (Exception e) { 
     Log.e("------------", e.toString()); 
    } 
} 

回答

0

在for循環中的if語句中;您可以使用正常編輯文本,如:

EditText et1 = (EditText)findViewById(R.id.editText1); 
     et1.setText(your ip address string); 

更具體地說,

這是你的代碼:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
      try { 
       for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { 
        NetworkInterface intf = en.nextElement(); 
        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { 
         InetAddress inetAddress = enumIpAddr.nextElement(); 
         if (!inetAddress.isLoopbackAddress()) { 
          /*TextView ipView= (TextView) findViewById(R.string.ip); 
          ipView.setText(inetAddress.getHostAddress());*/ 
          Log.e("IP------------", inetAddress.getHostAddress()); 

         //Here you can set the EditText and the TextView value. 
       TextView ipView= (TextView) findViewById(R.string.ip); 
       ipView.setText(inetAddress.getHostAddress()); 
         EditText et1 = (EditText)findViewById(R.id.editText1); 
      et1.setText(inetAddress.getHostAddress()); 

         } 
        } 
       } 
      } catch (Exception e) { 
       Log.e("------------", e.toString()); 
      } 

並且該值將被設置有.. :)

+0

日Thnx的幫助,但我想我的方法,並在工作,其實我已經設定相同的ID這兩個觀點,創造了一個力量密切的bug。現在解決了。 –

+0

沒有概率..高興,你想通了.. :) – mike20132013

+0

Thnx再次, –

相關問題