所以小伙子们,我正在创建一个应用程序,在这个布局中我展示了2个EditText,当用户点击其中任何一个时它会显示一个弹出菜单,当他们点击弹出菜单的一个项目时,它会设置edittext的文本到他们单击的项目的相同文本。这部分正在运作。但是我会有一个按钮,它会做某些事情,但我只是测试这个按钮,看看它是否正常工作,但是当我点击按钮时没有任何反应,我不知道为什么。这是代码:
public class horariosMenu extends Activity implements PopupMenu.OnMenuItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_horarios_menu);
Button buttonProcurar = (Button) findViewById(R.id.buttonProcurar);
final EditText deTextPartida = (EditText) findViewById(R.id.deText);
final EditText paraTextDestino = (EditText) findViewById(R.id.paraText);
buttonProcurar.setOnClickListener(
new Button.OnClickListener(){
@Override
public void onClick(View v) {
verifica(deTextPartida.getText().toString(), paraTextDestino.getText().toString());
}
}
);
}
public void showPopUp(View view){
PopupMenu popupMenu = new PopupMenu(this, view);
popupMenu.setOnMenuItemClickListener(horariosMenu.this);
MenuInflater menuInflater = popupMenu.getMenuInflater();
menuInflater.inflate(R.menu.popup_menu_locais, popupMenu.getMenu());
popupMenu.show();
}
public void showPopUpMenu_Para(View view){
PopupMenu popupMenuPara = new PopupMenu(this, view);
popupMenuPara.setOnMenuItemClickListener(horariosMenu.this);
MenuInflater menuinflaterPara = popupMenuPara.getMenuInflater();
menuinflaterPara.inflate(R.menu.popup_menu_locais_para, popupMenuPara.getMenu());
popupMenuPara.show();
}
@Override
public boolean onMenuItemClick(MenuItem item) {
EditText deText = (EditText) findViewById(R.id.deText);
EditText paraText = (EditText) findViewById(R.id.paraText);
switch(item.getItemId()){
case R.id.id_Coimbra:
deText.setText("Coimbra", null);
return true;
case R.id.id_SJoao:
deText.setText("S. Joao", null);
return true;
case R.id.id_SSilvestre:
deText.setText("S. Silvestre", null);
return true;
case R.id.id_SMartinho:
deText.setText("S. Martinho", null);
return true;
case R.id.id_Tentugal:
deText.setText("Tentugal", null);
return true;
case R.id.id_Carapinheira:
deText.setText("Carapinheira", null);
return true;
case R.id.id_Montemor:
deText.setText("Montemor", null);
return true;
case R.id.id_Maiorca:
deText.setText("Maiorca", null);
return true;
case R.id.id_Figueira:
deText.setText("Figueira da Foz", null);
return true;
//EDIT TEXT "PARA":
case R.id.id_Coimbra_Para:
paraText.setText("Coimbra", null);
return true;
case R.id.id_SJoao_Para:
paraText.setText("S. Joao", null);
return true;
case R.id.id_SSilvestre_Para:
paraText.setText("S. Silvestre", null);
return true;
case R.id.id_SMartinho_Para:
paraText.setText("S. Martinho", null);
return true;
case R.id.id_Tentugal_Para:
paraText.setText("Tentugal", null);
return true;
case R.id.id_Carapinheira_Para:
paraText.setText("Carapinheira", null);
return true;
case R.id.id_Montemor_Para:
paraText.setText("Montemor", null);
return true;
case R.id.id_Maiorca_Para:
paraText.setText("Maiorca", null);
return true;
case R.id.id_Figueira_Para:
paraText.setText("Figueira da Foz", null);
return true;
}
return false;
}
public void verifica(String a, String b) {
if (a == b) {
Toast.makeText(getBaseContext(), "Partida e destino nao podem ser iguais, escolha de novo!", Toast.LENGTH_LONG);
}
}
}
只有当EditText的文本相同时,按钮才会起作用,至少目前是这样。这就是为什么我调用verifica(字符串,字符串)函数,当我测试时,我把两者都放在一起,但它不起作用。 你们能检查一下发生了什么吗?
答案 0 :(得分:0)
可能你的按钮正常工作。您无法将两个字符串与==
进行比较。您必须使用compareTo
函数来比较字符串。
public void verifica(String a, String b) {
if (a.compareTo(b)) {
Toast.makeText(getBaseContext(), "Partida e destino nao podem ser iguais, escolha de novo!", Toast.LENGTH_LONG);
}
}
答案 1 :(得分:0)
必须从UI线程使用runOnUiThread
此外,您还需要展示吐司
Toast.makeText(...).show();
为了比较字符串,您应该使用equals