Android:如何在EditView和TextView中显示字符串数据

问题描述:

好吧,我有一个活动,用户知道他/她的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()); 
    } 
} 

在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再次, –