gpt4 book ai didi

java - 如何使用 if else 从 AutoCompleteTextView 获取字符串文本?

转载 作者:行者123 更新时间:2023-12-02 11:08:36 24 4
gpt4 key购买 nike

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
}

}

如何制作 if else 语句并设置 TextView hasil 来获取 res/value/string name sun

这是我第一次使用android studio编程,所以我对java移动代码不太了解。抱歉英语不好。希望您能理解

最佳答案

使用getText()获取AutoCompleteTextView值。以下是检查 AutoCompleteTextView 是否为空的条件的示例。

private void mpilih() {
if (!TextUtils.isEmpty(edit.getText())) {
hasil.setText(getString(R.string.sun));
} else {
edit.setError("Please enter your selection");
}
}

如果您仅使用那些固定项目作为选择,我建议您改用 Spinner..

编辑

对于多个“if”,您可以像这样使用它

    if (edit.getText().equals("Matahari")) {
hasil.setText(getString(R.string.sun));
} else if (edit.getText().equals("Bumi"){
hasil.setText(getString(R.string.earth));
}

关于java - 如何使用 if else 从 AutoCompleteTextView 获取字符串文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50752637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com