Android Studio 如何使用相同的Edittext作为两个不同的Android?

sulc1iza  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(186)

你好,我是Android开发的初学者。我想在EditText中显示来自xampp(phpmyadmin)的数据,如果EditText值不等于“"(来自xampp的数据!="”),如果等于“"(来自xampp的数据=="”),我想让用户键入该值。{如果显示该值,它应该对用户不可编辑}我尝试了if.. else
`

if(DepartStation_txt!=""){
   DS.setText(DepartStation_txt);
   DS.setEnabled(false);
}else {
    DS.setText("Enter the Value");
    DS.setEnabled(true);
}

'其中DS是编辑文本DS=

(EditText) findViewById(R.id.DS);

在onCreate中
执行此工作的任何功能以及不工作的原因

dxpyg8gm

dxpyg8gm1#

只需使用String类的方法来比较字符串。现在,首先需要从EditText控件读取文本,然后检查所需的字符串是否存在。对于您的情况,示例代码如下:

String DepartStation_txt;  // I don't know where do you get it from
String currentString = DS.getText().toString; // Read the current string first, if any 

if(currentString != null) {
    if(!currentString.isEqual(DepartStation_txt)) {
        DS.setText(DepartStation_txt);
        DS.setEnebled(false);
    }
    else {
        DS.setHint("Enter the value");
        DS.setEnabled(true);
    }
}
else {
// EditText has no string at all, user has to enter the value??
    DS.setHint("Enter the value");
    DS.setEnabled(true);
}

请注意,我使用setHint方法而不是setText方法,因为它更适合指导用户。

相关问题