我创建了一个屏幕,它将询问密码并与内置密码进行比较并启动另一个屏幕,但即使我提供了正确的密码,我也无法启动活动。任何帮助将不胜感激。
public class MainActivity extends Activity {
public static String lock = "vamsi";
public static String locker;
EditText et;
Button b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText1);
b = (Button) findViewById(R.id.button1);
locker = et.getText().toString();
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (locker.equals(lock)) {
Intent intent = new Intent("screen");
startActivity(intent);
}
}
});
}
}
答案 0 :(得分:1)
撰写以下行
Intent intent = new Intent (getApplicationContext(), FileName.class);
而不是
Intent intent = new Intent("screen");
FileName 是您要在Onclick方法中打开的活动的名称。
答案 1 :(得分:1)
我认为你是新蜜蜂,所以在开始开发之前你必须阅读一些理论并加上一些教程。根据您的代码,我假设您不知道如何致电intent
。
看这里,Intent是什么?
您的onclick代码应如下所示:
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
locker = et.getText().toString();
if (locker.equalsIgnoreCase(lock)) {
Intent intent = new Intent(getApplicationContext(),example.class);
//example.class is the name of activity which you want to launch.
startActivity(intent);
}
}
});
答案 2 :(得分:0)
首先,我想在此添加一些观点:
可能你可以试试这个:
if(locker.contains(lock)){
Intent success = new Intent(LoginScreen.this,MainMenu.class);
startActivity(success
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(LoginScreen.this);
builder.setMessage("Incorrect LoginId or Password");
builder.setCancelable(true);
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
}
});
builder.create().show();
}