我想比较android studio中的两个字符串。我有一个 textfield ,想要将此字符串与给定字符串进行比较。
为了更好的描述,我添加了一张照片。 我现有的字符串例如" A",我想现在检查,如果文本框包含此值,则按下按钮。
我试过了,但代码中有一个错误,我找不到......
final TextField textField = (TextField) findViewById(R.id.editText);
final String text1 = textField.getText().toString();
final String text2 = "A";
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Button2", "click");
if(text1.contains(text2)){
toast.show();
}
}
});
我希望,这可以改善我的第一个(坏)问题!
答案 0 :(得分:1)
试试这段代码
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
outerPanel = new javax.swing.JPanel();
listPanel = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
tablePanel = new javax.swing.JPanel();
jScrollPane2 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
outerPanel.setLayout(new java.awt.BorderLayout());
jLabel1.setText("Boolean Capability List");
jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", " " };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane1.setViewportView(jList1);
javax.swing.GroupLayout listPanelLayout = new javax.swing.GroupLayout(listPanel);
listPanel.setLayout(listPanelLayout);
listPanelLayout.setHorizontalGroup(
listPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(listPanelLayout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(0, 177, Short.MAX_VALUE))
.addComponent(jScrollPane1)
);
listPanelLayout.setVerticalGroup(
listPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(listPanelLayout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 15, Short.MAX_VALUE))
);
outerPanel.add(listPanel, java.awt.BorderLayout.NORTH);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane2.setViewportView(jTable1);
jButton1.setText("Save Values");
javax.swing.GroupLayout tablePanelLayout = new javax.swing.GroupLayout(tablePanel);
tablePanel.setLayout(tablePanelLayout);
tablePanelLayout.setHorizontalGroup(
tablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(tablePanelLayout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 303, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(tablePanelLayout.createSequentialGroup()
.addGap(101, 101, 101)
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
tablePanelLayout.setVerticalGroup(
tablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(tablePanelLayout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27)
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
outerPanel.add(tablePanel, java.awt.BorderLayout.SOUTH);
getContentPane().add(outerPanel, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JList jList1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTable jTable1;
private javax.swing.JPanel listPanel;
private javax.swing.JPanel outerPanel;
private javax.swing.JPanel tablePanel;
// End of variables declaration
}
答案 1 :(得分:1)
我猜你用Button2
声明了你的EditText
,继续...
您想要检查您提供的字符串和正在写EditText
的字符串是否与按下按钮时相同,您的代码是正确的但您需要比较{{的值1}}动态,所以你必须改变这个:
EditText
对此:
if(text1.contains(text2)){
toast.show();
}
希望这有点帮助:)
答案 2 :(得分:0)
方法仅包含检查text1
是否包含text2
。您应该使用equals()
方法。
答案 3 :(得分:0)
你可以做这样的事情
final EditText editText = (EditText) findViewById(R.id.editText);
final String text1 = editText.getText().toString();
final String text2 = "A";
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Button2", "click");
System.out.println("text1: " + text1);
System.out.println("text2: " + text2);
if (text1.length()>0&&!text1.equalsIgnoreCase("")) {
if(text1.equalsIgnoreCase(text2)){
toast.show();
}
} else {
System.out.println("text1 is null " );
}
}
});
答案 4 :(得分:0)
像这样使用
if (text1.equals(text2)) {
Toast.makeText(getApplicationContext(), "Equal", Toast.LENGTH_LONG)
.show();
}
答案 5 :(得分:0)
你需要这样的东西,因为当你比较文本时,你必须有一个更新版本的text.1。
如果你在开头问这个文本,那里面什么都没有。 您必须在每个OnClick事件中询问text1。
公开OnClick(查看v){
text1 = editText.getText().toString();
if(text1.equals(text2)){
//doStufg
}
}
答案 6 :(得分:0)
感谢所有好的和有用的答案! 我甚至尝试过某种方式,到目前为止效果很好。 谢谢大家在这里。干得好!
// editText.getText()。toString()。contains(“A”);