如何在不使用onClick的情况下调用函数

时间:2014-03-10 07:35:16

标签: android

我是学习android的初学者。在这里,我希望使用endfun(View v)方法调用onCreate(Bundle savedInstanceState)方法而不使用onClick。这里有人建议我。

  package com.example.loginandroid;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

import java.sql.SQLException;
import java.sql.Statement;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Looper;

public class MainActivity extends Activity{
    String username,password;     ResultSet rs =null;
    boolean temcfag=false,temqfag=true;
    public static String tag="Lifecycle activity";
    EditText user,pass;
    AlertDialog.Builder dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dialog=new AlertDialog.Builder(this);
        dialog.setNeutralButton("OK", null);


        user=(EditText)findViewById(R.id.editText1);
        pass=(EditText)findViewById(R.id.editText2);

        //i want call here endfun(View v) with out using onclick 
    }



    public void endfun(View v) throws SQLException{
        if(rs!=null){

             if (rs.next()) {
               Log.v("test#######","errorrrrrrrrrrr1");
                       setContentView(R.layout.activity_main1);
                    Log.v("test#######","errorrrrrrrrrrr1");
                     }
             else{

                  Log.v("test#######","errorrrrrrrrrrr2");
               dialog.setMessage("Your username and password are not valid");
                dialog.show();
                /*  Toast.makeText(getApplicationContext(), "UserName :"+username+"\n"+"password :"+password+"\n", Toast.LENGTH_LONG).show();

              Log.v("test#######","errorrrrrrrrrrr2");*/
             }}
            else{
                Log.v("out of loop","errorrrrrrrrrrr3");
            }
    }


}

2 个答案:

答案 0 :(得分:0)

您可以使用null作为参数:

endfun(null);

答案 1 :(得分:0)

在调用之后执行的第一个方法是onCreate,无论你想要什么。 为什么在不需要时传递视图。