Java 如何使用if-else从AutoCompleteTextView获取字符串文本? 公共类Cari扩展了AppCompatActivity,实现了TextWatcher、View.OnClickListener{ TextView hasil、teks、teks2; 自动完成文本视图编辑; 上一个按钮,第三个按钮; String[]item={“Matahari”、“Merkurius”、“Venus”、“Bumi”、“Mars”、“Yupiter”、“Saturnus”、“Uranus”、“Neptunus”}; hasil=findviewbyd(R.id.hasil); edit=findviewbyd(R.id.edit); edit.addTextChangedListener(此文件); edit.setAdapter(新的ArrayAdapter(这个,android.R.layout.simple_下拉列表,项目,项目)); teks=findViewById(R.id.title); Typeface customfont=Typeface.createFromAsset(getAssets(),“font/Starjout.ttf”); teks.setTypeface(自定义字体); teks2=findViewById(R.id.标题); Typeface customfont2=Typeface.createFromAsset(getAssets(),“font/Starjout.ttf”); teks2.setTypeface(自定义字体2); terpilih=findviewbyd(R.id.pilih); terpilih.setOnClickListener(此); } public void onTextChanged(字符序列、int start、int before、int count){ edit.getText(); } 更改前文本之前的公共void(字符序列s、int start、int count、int after){ //不用 } 公共无效后文本已更改(可编辑){ //不用 } 公共void onClick(视图a){ 开关(a.getId()){ 案例R.id.pilih: Toast.makeText(这是“Kamu memilih Planet”+edit.getText(),Toast.LENGTH\u SHORT.show(); mpilih(); 打破 } } 私有void mpilih(){ if(//应该添加什么语句){ //这里也是*hasil.setText(); } 否则{ //这里也是 } }
如何使用if-else语句并设置TextView-hasil以获取res/value/string-name-sun 这是我第一次使用android studio编程,所以我对java mobile代码了解不多。对不起,英语不好。希望您理解使用Java 如何使用if-else从AutoCompleteTextView获取字符串文本? 公共类Cari扩展了AppCompatActivity,实现了TextWatcher、View.OnClickListener{ TextView hasil、teks、teks2; 自动完成文本视图编辑; 上一个按钮,第三个按钮; String[]item={“Matahari”、“Merkurius”、“Venus”、“Bumi”、“Mars”、“Yupiter”、“Saturnus”、“Uranus”、“Neptunus”}; hasil=findviewbyd(R.id.hasil); edit=findviewbyd(R.id.edit); edit.addTextChangedListener(此文件); edit.setAdapter(新的ArrayAdapter(这个,android.R.layout.simple_下拉列表,项目,项目)); teks=findViewById(R.id.title); Typeface customfont=Typeface.createFromAsset(getAssets(),“font/Starjout.ttf”); teks.setTypeface(自定义字体); teks2=findViewById(R.id.标题); Typeface customfont2=Typeface.createFromAsset(getAssets(),“font/Starjout.ttf”); teks2.setTypeface(自定义字体2); terpilih=findviewbyd(R.id.pilih); terpilih.setOnClickListener(此); } public void onTextChanged(字符序列、int start、int before、int count){ edit.getText(); } 更改前文本之前的公共void(字符序列s、int start、int count、int after){ //不用 } 公共无效后文本已更改(可编辑){ //不用 } 公共void onClick(视图a){ 开关(a.getId()){ 案例R.id.pilih: Toast.makeText(这是“Kamu memilih Planet”+edit.getText(),Toast.LENGTH\u SHORT.show(); mpilih(); 打破 } } 私有void mpilih(){ if(//应该添加什么语句){ //这里也是*hasil.setText(); } 否则{ //这里也是 } },java,android,Java,Android,如何使用if-else语句并设置TextView-hasil以获取res/value/string-name-sun 这是我第一次使用android studio编程,所以我对java mobile代码了解不多。对不起,英语不好。希望您理解使用getText()获取AutoCompleteTextView值。 下面是检查条件的示例,如果AutoCompleteTextView为空 public class Cari extends AppCompatActivity implements Tex
getText()
获取AutoCompleteTextView
值。
下面是检查条件的示例,如果AutoCompleteTextView
为空
public class Cari extends AppCompatActivity implements TextWatcher,View.OnClickListener {
TextView hasil, teks, teks2;
AutoCompleteTextView edit;
Button prev, terpilih;
String[] item = { "Matahari","Merkurius","Venus","Bumi","Mars","Yupiter","Saturnus","Uranus","Neptunus" };
hasil = findViewById(R.id.hasil);
edit = findViewById(R.id.edit);
edit.addTextChangedListener(this);
edit.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, item));
teks = findViewById(R.id.title);
Typeface customfont = Typeface.createFromAsset(getAssets(), "font/Starjout.ttf");
teks.setTypeface(customfont);
teks2 = findViewById(R.id.titleDes);
Typeface customfont2 = Typeface.createFromAsset(getAssets(), "font/Starjout.ttf");
teks2.setTypeface(customfont2);
terpilih = findViewById(R.id.pilih);
terpilih.setOnClickListener(this);
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
edit.getText();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//not used
}
public void afterTextChanged(Editable s) {
//not used
}
public void onClick(View a) {
switch (a.getId()) {
case R.id.pilih:
Toast.makeText(this, "Kamu memilih Planet "+edit.getText(), Toast.LENGTH_SHORT).show();
mpilih();
break;
}
}
private void mpilih() {
if (//what statement should add) {
//here too *hasil.setText();
}
else {
//here too
}
}
如果您只使用这些固定项作为选择,我建议您使用微调器
编辑
对于多个“如果”,您可以这样使用它
private void mpilih() {
if (!TextUtils.isEmpty(edit.getText())) {
hasil.setText(getString(R.string.sun));
} else {
edit.setError("Please enter your selection");
}
}
该代码不适用于多个“if”。代码只读取一个data@JS4多个“if”示例。该代码不读取hasil.setText(getString(R.string.sun));它直接编码:else{edit.setError(“请输入您的选择”);}
if (edit.getText().equals("Matahari")) {
hasil.setText(getString(R.string.sun));
} else if (edit.getText().equals("Bumi"){
hasil.setText(getString(R.string.earth));
}