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

Android:如何在EditView和TextView中显示字符串数据,android,view,ip,Android,View,Ip,好的,我有一个活动,在其中用户可以知道他/她的IP地址,IP显示在TextView中,但我希望它显示在EditView中,以便轻松复制粘贴。这是我的密码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ip); getWindow ().setSof

好的,我有一个活动,在其中用户可以知道他/她的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());
    }
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ip);
获取窗口
()设置输入模式(
WindowManager。
LayoutParams.SOFT_输入_状态_始终_隐藏);
试一试{
对于(枚举en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){
NetworkInterface intf=en.nextElement();
对于(枚举Enumeration EnumipAddress=intf.getInetAddresses();EnumipAddress.hasMoreElements();){
InetAddress InetAddress=enumIpAddr.nextElement();
如果(!inetAddress.isLoopbackAddress()){
TextView ipView=(TextView)findViewById(R.string.ip);
setText(inetAddress.getHostAddress());
}
}
}
}捕获(例外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());
            }
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT\u输入\状态\始终\隐藏);
试一试{
对于(枚举en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){
NetworkInterface intf=en.nextElement();
对于(枚举Enumeration EnumipAddress=intf.getInetAddresses();EnumipAddress.hasMoreElements();){
InetAddress InetAddress=enumIpAddr.nextElement();
如果(!inetAddress.isLoopbackAddress()){
/*TextView ipView=(TextView)findViewById(R.string.ip);
setText(inetAddress.getHostAddress())*/
Log.e(“IP-------”,inetAddress.getHostAddress());
//您可以在此处设置EditText和TextView值。
TextView ipView=(TextView)findViewById(R.string.ip);
setText(inetAddress.getHostAddress());
EditText et1=(EditText)findViewById(R.id.editText1);
setText(inetAddress.getHostAddress());
}
}
}
}捕获(例外e){
Log.e(“----------”,e.toString());
}

值将在此处设置:)

Thnx感谢您的帮助,但我尝试了我的方法,并且成功了,实际上我在两个视图中设置了相同的id,创建了一个强制关闭错误。现在已经解决了,没问题。。我很高兴你找到了答案……)