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

时间:2014-03-14 02:27:48

标签: android view ip

好的,我有一个用户了解他/她的IP地址的活动,IP显示在TextView但我希望它显示在EditView中以便于复制粘贴。这是我的代码......

@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());
    }
}

1 个答案:

答案 0 :(得分: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());
            }

值将在那里设置..:)