我有一个带有 3 个按钮创建、显示和设置的布局。单击创建和显示按钮后,它们工作正常并转移到下一个活动,无论它是什么。但我需要检查设置按钮。也就是说 - 如果密码表存在,那么它应该转到 password.class,如果不存在,那么它应该转到 Setting.class。
在尝试此代码时,else 语句显示无效代码并且没有响应。
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity extends Activity {
Button create, show, setting;
String pass1,pass2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
create = (Button)findViewById(R.id.button1);
create.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent1 = new Intent(view.getContext(), Create.class);
startActivityForResult(myIntent1, 0);
}
});
show = (Button)findViewById(R.id.button2);
show.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent2 = new Intent(view.getContext(), Password.class);
startActivityForResult(myIntent2, 0);
}
});
SQLiteDatabase db = openOrCreateDatabase("passmanage", MODE_PRIVATE, null);
Cursor c = db.rawQuery("select * from password", null);
c.moveToFirst();
if(c!=null)
{
setting = (Button)findViewById(R.id.button3);
setting.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent3 = new Intent(view.getContext(), Password.class);
startActivityForResult(myIntent3, 0);
}
});
}
else
{
setting = (Button)findViewById(R.id.button3);
setting.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent3 = new Intent(view.getContext(), Setting.class);
startActivityForResult(myIntent3, 0);
}
});
}
}
}
我也试过这种方式-
setting = (Button)findViewById(R.id.button3);
setting.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
SQLiteDatabase db = openOrCreateDatabase("passmanage", MODE_PRIVATE, null);
Cursor c = db.rawQuery("select * from password", null);
c.moveToFirst();
if(c!=null)
{
Intent myIntent3 = new Intent(view.getContext(), Password.class);
startActivityForResult(myIntent3, 0);
}
else
{
Intent myIntent3 = new Intent(view.getContext(), Setting.class);
startActivityForResult(myIntent3, 0);
}
}});
但在这里我也收到错误作为其他条件的死代码。
我也试过这种方式
setting = (Button)findViewById(R.id.button3);
setting.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
SQLiteDatabase db = openOrCreateDatabase("passmanage", MODE_PRIVATE, null);
Cursor c = db.rawQuery("select * from password", null);
c.moveToFirst();
if(c!=null)
{
do
{
pass1 = c.getString(c.getColumnIndex("pass"));
pass2 = c.getString(1);
} while(c.moveToNext());
if(pass1.compareTo("")==0 && pass2.compareTo("")==0)
{
Intent myIntent3 = new Intent(view.getContext(), Password.class);
startActivityForResult(myIntent3, 0);
}
else
{
Intent myIntent3 = new Intent(view.getContext(), Setting.class);
startActivityForResult(myIntent3, 0);
}
}
}});
但它意外地关闭了!
我还有一个小疑问。我是否需要为同一活动中的不同按钮创建具有不同名称的意图?