使用按钮启动活动

时间:2014-04-22 08:44:10

标签: android

我试图配置button1来改变活动,但是虽然我没有用eclipse检测到错误,但按钮不起作用,有人可以告诉我什么是错误的。 在lprimo按钮之后有'另一个数据备份按钮,我不认为问题是由另一个按钮的存在引起的。

public class Aggiungi extends Activity implements OnClickListener {
        private Button btn_save;
        private EditText edit_turno,edit_ore;
        private TextView edit_mese,edit_anno;
        private DbHelper mHelper;
        private SQLiteDatabase dataBase;
        private String idturno,anno,mese,turno,ore;
        private boolean isUpdate;
        Spinner selectedMesi,selectedGiorni;
        private final static String MY_PREFERENCES = "MyPref";
        private final static String ANNO = "anno";

            public void cambia (View view){
            final Intent cambioTermini;
            cambioTermini = new Intent (this, Cambio.class);
            Button button=(Button)findViewById(R.id.button1);
            button.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    startActivity(cambioTermini);
                }
            });
            }
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.add_activity);
                SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
                Intent intent=getIntent();
String anno = prefs.getString(ANNO, "2014");


        btn_save=(Button)findViewById(R.id.save_btn);
        edit_mese=(TextView)findViewById(R.id.txtmese);
        edit_anno=(TextView)findViewById(R.id.txtanno);
        edit_turno=(EditText)findViewById(R.id.turno_editTxt);
        edit_ore=(EditText)findViewById(R.id.ore_editTxt);

       isUpdate=getIntent().getExtras().getBoolean("update");
        if(isUpdate)
        {
            idturno=getIntent().getExtras().getString("ID");
            mese=getIntent().getExtras().getString("Mese");
            anno=getIntent().getExtras().getString("Anno");
            turno=getIntent().getExtras().getString("Turno");
            ore=getIntent().getExtras().getString("Ore");
            edit_mese.setText(mese);
            edit_anno.setText(anno);
            edit_turno.setText(turno);
            edit_ore.setText(ore);

        }

         btn_save.setOnClickListener(this);

         mHelper=new DbHelper(this);

    }

    // saveButton click event 
    public void onClick(View v) {
        mese=edit_mese.getText().toString().trim();
        anno=edit_anno.getText().toString().trim();
        turno=edit_turno.getText().toString().trim();
        ore=edit_ore.getText().toString().trim();
        if(anno.length()>0&& mese.length()>0 && turno.length()>0 &&ore.length()>0)
        {
            saveData();
        }
        else

3 个答案:

答案 0 :(得分:3)

根据我的理解,你想要推出“Cambio”#39;按钮1上的活动单击。试试这个。

public class Aggiungi extends Activity implements OnClickListener {

private Button btn_save;
private EditText edit_turno,edit_ore;
private TextView edit_mese,edit_anno;
private DbHelper mHelper;
private SQLiteDatabase dataBase;
private String idturno,anno,mese,turno,ore;
private boolean isUpdate;
Spinner selectedMesi,selectedGiorni;
private final static String MY_PREFERENCES = "MyPref";
private final static String ANNO = "anno";

public void cambia(){
final Intent cambioTermini;
cambioTermini = new Intent (this, Cambio.class);

// Start cambioTermini activity
startActivity(cambioTermini);

// close this activity
finish();
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_activity);
SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
Intent intent=getIntent();
}

@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
    cambia();
    break;
}
}   
}

答案 1 :(得分:0)

我期待的是:

  public void cambia (View view){
    final Intent cambioTermini;
    cambioTermini = new Intent (this, Cambio.class);
    startActivity(cambioTermini);
}

因为你有

<Button
android:id="@+id/button1"
//.. other paramters
android:onClick="cambia"
android:text="cambia" />

答案 2 :(得分:0)

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.ImageView;
 import android.widget.Toast;

     public class Contact extends Activity
       {

public ImageView btnlog1;

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

    btnlog1=(ImageView)findViewById(R.id.imageView2);
            btnlog1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent l = new Intent(Contact.this,                  
                       AndroidDashboardDesignActivity.class);
            startActivity(l);
            Contact.this.finish();
        }
    });



 }



      }

使用此代码并尝试这样..这是一个简单的代码,用于将一个活动更改为另一个活动..让我知道你的问题..