点击按钮 - Android后转到另一个活动

时间:2015-07-06 21:01:27

标签: android google-app-engine android-activity android-studio

我的Android应用程序中的登录活动有onClickListener方法:

btnLogin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(txtEmail.getWindowToken(), 0);
                imm.hideSoftInputFromWindow(txtPassword.getWindowToken(), 0);
                String password = txtPassword.getText().toString();
                String email = txtEmail.getText().toString();
                if ((txtEmail.length() == 0) || (txtPassword.length() == 0)) {
                    Toast.makeText(LoginMember.this, "You need to provide values for Email and Password", Toast.LENGTH_SHORT).show();
                    return;
                }

                //Go ahead and perform the transaction
                String[] params = {email,password};
                new EndpointsAsyncTaskInsert(LoginMember.this).execute(params);


            }
        });

它将数据发送到Google App Engine,没有任何问题,已经定义了我的EndpointsAsyncTask类等等

现在,我的问题是,在此之后我还需要去另一个活动,我不能确定,但​​是如果我记得很清楚,我可以在使用SQLite登录时自动执行此操作,不要这样做。我知道如何在这里完成它。

我已经在清单上宣布了我需要的活动。

它应该是这样的:

@Override
public void onClick(View v){
    Intent intent = new Intent(LoginMember.this, WelcomeScreen.class);
    startActivity(intent);
 }

我的问题是我不知道如何添加"或"追加"这个活动交易成了这个逻辑,我对Android和谷歌应用程序引擎相当新,任何想法???

提前致谢!

修改

这是我的EndpointsAsyncTaskInsert代码:

package com.kkoci.shairlook;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.appspot.shairlook1.userEndpoint.UserEndpoint;
import com.appspot.shairlook1.userEndpoint.model.User;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
* Created by kristian on 04/07/2015.
*/

public class EndpointsAsyncTaskInsert extends AsyncTask<String, Void, User> implements GoogleClientRequestInitializer {
private static UserEndpoint myApiService = null;
private Context context;

EndpointsAsyncTaskInsert(Context context) {
    this.context = context;
}

@Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
    // put it here no in MyClass
    abstractGoogleClientRequest.setDisableGZipContent(true);
}

// class MyClass{} // you don't need it


@Override
protected User doInBackground(String... params) {
    User response = null;
    if (myApiService == null) { // Only do this once
        UserEndpoint.Builder builder = new UserEndpoint.Builder(AndroidHttp.newCompatibleTransport(),
                new AndroidJsonFactory(), null)
// options for running against local devappserver
// - 10.0.2.2 is localhost's IP address in Android emulator
// - turn off compression when running against local devappserver
                .setRootUrl("https://shairlook1.appspot.com/_ah/api/")
                .setGoogleClientRequestInitializer(this);
// end options for devappserver

        myApiService = builder.build();
    }

    try {
        User users = new User();
        users.setEmail(params[0]);
        users.setPassword(params[1]);
        users.setName(params[2]);
        response = myApiService.insertUser(users).execute();
    } catch (Exception e) {
        Log.d("Could not Add User", e.getMessage(), e);
    }
    return response;
}

}

第二次编辑

这就是它现在的样子,它给了我'java.lang.NoClassDefFoundError&#39;在这一行:new EndpointsAsyncTaskInsert(LoginMember.this) {

public class LoginMember extends Activity {
private static
//DbAdapter dbAdapter = null;
    //EditText txtUserName;
    EditText txtPassword;
    EditText txtEmail;
    Button btnLogin;
    TextView Forgot_text;
    Button twitter;
    Button facebook;
    //Button btnRegister;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        txtPassword = (EditText) findViewById(R.id.et_pw);
        txtEmail = (EditText) findViewById(R.id.et_email);
        btnLogin = (Button) findViewById(R.id.btn_login);
        twitter = (Button) findViewById(R.id.twitter);
        facebook = (Button) findViewById(R.id.facebook);
        Forgot_text = (TextView) findViewById(R.id.Forgot_text);

        btnLogin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(txtEmail.getWindowToken(), 0);
                imm.hideSoftInputFromWindow(txtPassword.getWindowToken(), 0);
                String password = txtPassword.getText().toString();
                String email = txtEmail.getText().toString();
                if ((txtEmail.length() == 0) || (txtPassword.length() == 0)) {
                    Toast.makeText(LoginMember.this, "You need to provide values for Email and Password", Toast.LENGTH_SHORT).show();
                    return;
                }

                //Go ahead and perform the transaction
                String[] params = {email,password};
                //new EndpointsAsyncTaskInsert(LoginMember.this).execute(params);
                /**try{  Intent k = new Intent(LoginMember.this, WelcomeScreen.class);
                    startActivity(k);
                }catch(Exception e){

                }**/
                new EndpointsAsyncTaskInsert(LoginMember.this) {
                    protected void onPostExecute(User result) {
                        super.onPostExecute(result);
                        // Do something with result
                        Intent intent = new Intent(LoginMember.this, WelcomeScreen.class);
                        startActivity(intent);
                    }
                }.execute(params);

            }
        });

    }
public void getUser(View v) {
    new EndpointsAsyncTask(this).execute();
}
public void insertUser(View v) {
    new EndpointsAsyncTaskInsert(this).execute();
}

}

4 个答案:

答案 0 :(得分:2)

好吧,我明白了,也许你应该这样做,我还没试过这个,但可以帮助你:

在onCreate方法之前,以这种方式声明var:

Activity currentActivity;

然后在onCreate方法中执行此操作:

currentActivity=this;

那么,当你让Asyctask,你做这个:

new EndpointsAsyncTaskInsert(currentActivity.getApplicationContext()).execute(params);

希望有所帮助,让我知道是否有帮助,如果不是,我会尝试以另一种方式帮助你。

问候。

答案 1 :(得分:1)

您可以创建AsyncTask类的匿名版本,并覆盖onPostExecute以在完成后启动新活动。

new EndpointsAsyncTaskInsert(LoginMember.this) {
    protected void onPostExecute(User result) {
        super.onPostExecute(result);
        // Do something with result
        Intent intent = new Intent(LoginMember.this, WelcomeScreen.class);
        startActivity(intent);
    }
}.execute(params);

答案 2 :(得分:1)

在EndpointsAsyncTask类中,应该有一个名为onPostExecute()的方法,该方法在异步任务完成时执行。在这里,您应该通知您的活动去另一项活动。

有很多方法可以做到这一点。

  1. 您可以为实例

    创建一个Interface类

    public interface OnTaskFinishListener {

     void onFinish();
    

    }

  2. 然后在调用者类中实现此接口:

    public class YourActivity extends Activity implements OnTaskFinishListener {
    
        void onFinish(){
             Intent intent = new Intent(LoginMember.this, WelcomeScreen.class);
             startActivity(intent);
        }
    }
    

    创建asynctask时,应将此引用作为参数传递给构造函数,并将其保存在任务字段中,并在完成任务时调用onFinish方法。

    public EndpointsAsyncTaskInsert extends AsyncTask...{
        private OnTaskFinishListener listener;
    
        public EndpointsAsyncTaskInsert(OnTaskFinishListener listener){
            this.listener = listener;
        }
    
        protected void onPostExecute(..){
             //notify the listener
             listener.onFinish();
        }
    }
    
    1. 第二种和更松散耦合的方式是使用事件总线库,例如greenrobots EventBus https://github.com/greenrobot/EventBus,然后您可以在任务完成时发布事件,然后您可以在活动中接收该事件没有设置任何听众。

答案 3 :(得分:0)

也许这可能有效

if ((txtEmail.length() == 0) || (txtPassword.length() == 0)) {
                Toast.makeText(LoginMember.this, "You need to provide values for Email and Password", Toast.LENGTH_SHORT).show();
                return;
            }
else{
String[] params = {email,password};
            new EndpointsAsyncTaskInsert(LoginMember.this).execute(params);
}

在扩展Asynctask的类中覆盖onpostexecute方法并添加以下代码

 Intent in=new Intent(Login.this,Welcome.class);
 in.putExtra("email",email);
 in.putExtra("password",password);
 startActivity(in);

您可以使用bundle将数据从一个活动发送到另一个活动,并从Welcome活动

中的包中检索
Intent in=getIntent();
String email=in.getStringExtra("email");