简体   繁体   English

我如何在此代码中插入intent方法,当单击按钮时它将显示第二个活动

[英]How i insert intent method to this code for when click button it will show second activity

Here is the java activity that provide to add data for record in SQLite. 以下是为SQLite中的记录添加数据而提供的java活动。 My question is how i insert some code for when clicking button and it will show second activity. 我的问题是如何在单击按钮时插入一些代码,它将显示第二个活动。 i don't know where i should insert. 我不知道应该在哪里插入。 And don't know what the code should be. 并且不知道代码应该是什么。 please help me. 请帮我。

import java.sql.PreparedStatement;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class AddStudent extends Activity {
DatabaseStudent mHelper;
SQLiteDatabase mDb;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add);

    mHelper = new DatabaseStudent(this);
    mDb = mHelper.getWritableDatabase();

    final EditText editName = (EditText)findViewById(R.id.editName);
    final EditText editLastName = (EditText)findViewById(R.id.editLastName);


    ImageView buttonAdd = (ImageView)findViewById(R.id.imageAdd);

    buttonAdd.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            String name = editName.getText().toString();
            String lastname = editLastName.getText().toString();
            String condition = getIntent().getStringExtra("Condition");
            double school = getIntent().getDoubleExtra("Intent", 0);

            //Date&Time
            java.util.Date dt = new java.util.Date();
            java.text.SimpleDateFormat sdf = 
                 new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String currentTime = sdf.format(dt);

            if(name.length() != 0 && lastname.length() != 0 
                     ) {//&& school.length() != 0

                Cursor mCursor = mDb.rawQuery("SELECT * FROM " 
                        + DatabaseStudent.TABLE_NAME + " WHERE " 
                        + DatabaseStudent.COL_NAME + "='" + name + "'" 
                        + " AND " + DatabaseStudent.COL_LASTNAME + "='" 
                        + lastname + "'" + " AND " 
                        + DatabaseStudent.COL_SCHOOL + "='" + school //add COL_SCHOOL = currentTime
                        + "'"+ " AND " + DatabaseStudent.COL_TIME + "='" + currentTime
                        + "'"+ " AND " + DatabaseStudent.COL_CON + "='" + condition
                        + "'", null);

                if(mCursor.getCount() == 0) {
                    mDb.execSQL("INSERT INTO " + DatabaseStudent.TABLE_NAME 
                            + " (" + DatabaseStudent.COL_NAME 
                            + ", " + DatabaseStudent.COL_LASTNAME 
                            + ", " + DatabaseStudent.COL_SCHOOL 
                            + ", " + DatabaseStudent.COL_TIME 
                            + ", " + DatabaseStudent.COL_CON 
                            + ") VALUES ('" + name + "', '" + lastname 
                            + "', '" + school + "', '" + currentTime + "', '" + condition + "');");



                    editName.setText("");
                    editLastName.setText("");


                    Toast.makeText(getApplicationContext()
                            , "Already added"
                            , Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext()
                            , "This data is exist"
                            , Toast.LENGTH_SHORT).show();
                }

            } else {
                Toast.makeText(getApplicationContext()
                        , "Please fill in the blank"
                        , Toast.LENGTH_SHORT).show();
            }
        }
    });

}

public void onStop() {
    super.onStop();
    mHelper.close();
    mDb.close();
}

} }

You'd want to insert any code you want executed when you hit a button in the onClick method, which you have as seen here: 当您点击onClick方法中的按钮时,您想要插入要执行的任何代码,如下所示:

buttonAdd.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

    // do stuff when buttonAdd is clicked

    }
});

Now you can use intents inside the onClick method to begin a second activity, like so: 现在,您可以在onClick方法中使用意图来开始第二个活动,如下所示:

buttonAdd.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

        Intent intent = new Intent(current_activity.this, second_activity.class);
        startActivity(intent);
    }
});

You can consult the following for more details: http://developer.android.com/training/basics/firstapp/starting-activity.html 有关更多详细信息,请参阅以下内容: http//developer.android.com/training/basics/firstapp/starting-activity.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击按钮时如何破坏活动? - How to destroy an activity when I click a button? 当第二个活动包含图像按钮时,意图不起作用 - intent not working when second activity contained image button Android Studio:如何在我的第二个活动中看到intent方法携带的信息? - Android Studio: how can I see the information carried through the intent method on my second activity? 当我打算获取 arrayList 的数据时,如何在第二个活动和 setText 和 Image 中获取这些数据? - When I intent the arrayList's data, and how can I get those data in the second activity and setText and Image? 单击按钮时的意图错误 - 为什么? - Intent error when I click the button - why? 如何在Android Studio中从活动到片段单击意图 - how to intent with on click button from activity to fragment in android studio 单击通知中的按钮时如何更新活动 UI? - How to update activity UI when i click a button in my notification? 我正在尝试在第二个活动中显示暂停按钮 - I am trying to show pause button in second activity 当我单击按钮打开此活动时,它关闭 - When I click the button to open this activity it closes 当我单击MainActivity中的按钮以显示其他活动时,它不起作用 - It doesn't work when i click on a button in MainActivity in order to show other activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM