带标签的SWITCH条件

时间:2014-12-27 03:52:51

标签: android

我正在使用两个标签执行程序,一个用于显示使用IF条件和另一个SWITCH条件的示例。 除了通过SWITCH验证字符串外,一切正常。已经将EditText转换为String仍然无法正常工作。当我输入条件的值时," felpudo"或者" Felpudo"总是返回错误的密码。

    public void botaonum1(View v){
     String senhaEdit = senha.getText().toString();
            switch(senhaEdit){
                case "felpudo":
                result1.setText("Correct Password!");
                case "Felpudo":
                result1.setText("Correct Password!");
                default:
                result1.setText("Incorrect Password!");
    }

}

我的Java代码:

public class MainActivity extends ActionBarActivity {

private EditText num;
private EditText senha;
private TextView result;
private TextView result1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    num = (EditText)findViewById(R.id.Id);
    senha = (EditText)findViewById(R.id.Id1);
    result = (TextView)findViewById(R.id.textfinal);
    result1 = (TextView)findViewById(R.id.textfinal1);

    TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
    tabHost.setup();

    TabSpec spec1=tabHost.newTabSpec("IF");
    spec1.setContent(R.id.IF);
    spec1.setIndicator("IF");

    TabSpec spec2=tabHost.newTabSpec("SWITCH");
    spec2.setContent(R.id.SWITCH);
    spec2.setIndicator("SWITCH");

    tabHost.addTab(spec1);
    tabHost.addTab(spec2);

}

public void botaonum(View v){
    String valorEdit = num.getText().toString();
    int numEdit = Integer.parseInt(valorEdit);
            if(numEdit%2==0)
                result.setText("O número é par");
            else
                result.setText("O número é ímpar");

}

public void botaonum1(View v){
     String senhaEdit = senha.getText().toString();
            switch(senhaEdit){
                case "felpudo":
                result1.setText("Correct Password!");
                case "Felpudo":
                result1.setText("Correct Password!");
                default:
                result1.setText("Incorrect Password!");
   }
   }
   }

My Button with onclick botaonum1 in XML:

<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="OK"
            android:id="@+id/button1"
            android:layout_below="@+id/Id"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="82dp"
            android:layout_gravity="center_horizontal"
            android:onClick="botaonum1"/>

0 个答案:

没有答案