import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
EditText username;
EditText password;
Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText) findViewById(R.id.editTextpaswrd);
password = (EditText) findViewById(R.id.editTextpssd);
Button login = (Button) findViewById(R.id.buttonlgin);
login.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v) {
String name=username.getText().toString();
String pwd=password.getText().toString();
switch(v.getId()) {
case R.id.buttonlgin;
break;
if (name.equals("nana") && pwd.equals("12345")) {
Intent i=new Intent(this, Qrcodescreen.class);
startActivity(i);
Toast.makeText(getBaseContext(), "Success!",
Toast.LENGTH_SHORT).show();
} else Toast.makeText(getBaseContext(), "Invalid Login Details", Toast.LENGTH_SHORT).show();
break;
}
displayNextAlert();
}
private void displayNextAlert() {
// Get what the user entered
EditText username = (EditText) findViewById(R.id.editTextpaswrd);
//Puts out an error message (!)
if ( username.getText().toString().length() == 0)
username.setError( "Username required");
EditText password = (EditText) findViewById(R.id.editTextpssd);
if ( password.getText().toString().length() == 0)
password.setError( "Password required");
}
}
我在'case R.id.buttonlgn;'上遇到错误。请帮忙。感谢
答案 0 :(得分:2)
替换 案例R.id.buttonlgin;与
case R.id.buttonlgin:
注意':'
答案 1 :(得分:2)
在case R.id.buttonlgin
之后一定不能有半结肠,它必须是冒号。
即用case R.id.buttonlgin;
替换case R.id.buttonlgin:
并删除第一次出现的断裂;在您的代码中使代码工作
答案 2 :(得分:1)
switch(v.getId()) {
case R.id.buttonlgin:
break;
}
答案 3 :(得分:1)
你必须把":"而不是";"在R.id.buttonlgin之后。
switch(v.getId()) {
case R.id.buttonlgin:
break;
}