0

我有两个活动,第一个活动“LoginActivity”,第二个活动“student_activity”。请告诉我如何从第二个活动调用方法“调用”并将值 bool 返回到第一个活动,以便知道用户是否 id 和密码正确或不正确。第一个活动从“edittext”获取 ID 和密码,然后将 ID 和密码发送到第二个活动中的方法,以确保从服务器的数据。

代码第一个活动是:

    public class LoginActivity extends Activity{
        EditText EdtId;
        EditText EdtPassword;
        Button btn1;
        SharedPreferences prefs;
          @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.login);
                 prefs = this.getSharedPreferences(this.getApplicationContext().getPackageName(), Context.MODE_PRIVATE);
                EdtId=(EditText)findViewById(R.id.IdStudent);
                EdtPassword=(EditText)findViewById(R.id.PasswordStudent);
                 btn1=(Button)findViewById(R.id.btnLogin);

                 btn1.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        createLoginSession(Integer.parseInt(EdtId.getText().toString()),EdtPassword.getText().toString());
//here call second activity for sure from data 
                         Intent intent = new Intent(LoginActivity.this,tofi.android.Student_Activity.class);
                            startActivity(intent);      
                            finish();
                        startActivity(new Intent(LoginActivity.this,com.jcxavier.widget.test.BadgeButtonTestActivity.class));
                    }
                });   
            }
    //this method store data in SharedPreferences for get this data in second activity 
         public void createLoginSession(int id, String password){
             SharedPreferences.Editor editor = prefs.edit();
             editor.putInt("id", id);
             editor.putString("password", password);
             editor.commit();
            }
    }

代码第二个活动是:

   public class Student_Activity {
SharedPreferences prefs = this.getSharedPreferences(this.getApplicationContext().getPackageName(), Context.MODE_PRIVATE);
     public void onCreate(Bundle icicle) {
                super.onCreate(icicle);
                setContentView(R.layout.program_student);
                NewsLoader call = new NewsLoader();
    call.execute(this, true);       
            }
               private Context context;
            private boolean pullFromServer = false;
            class NewsLoader extends AsyncTask<Object, Void, List<student>> {
                private Context context;
                private boolean pullFromServer = false;

                protected List<student> doInBackground(Object... params) {
                    context = (Context) params[0];
                    pullFromServer = (Boolean) params[1];
                    dataSource.open();
                    if (pullFromServer) {
    //get attribute from SharedPreferences  
                        int id = prefs.getInt("id", 24);
                        String password = prefs.getString("password","wce");
    // studentHandler class for sure password content method call for send to server and //return value if correct or not correct and return value type bool.
                        bool s;
                        s = StudentHandler.getInstance().call(context,id,password);
        } 
        }
     }
4

1 回答 1

4

1.在第一个活动中调用startActivityForResult()文档)和覆盖onActivityResult()文档)。

2.在第二个活动中执行您需要做的任何验证(这也可以在第一个活动中通过传递数据来完成Intent)和调用setResult(int resultCode, Intent data)文档),然后finish();从第二个活动。

如果使用startActivityForResult()不适合您的情况,那么您可以简单地使用setResult()和 startActivity(),通过 Intent 传递您需要的任何数据,并在onActivityResult().

我只是略过这个,但这里有一个例子

于 2013-05-15T01:48:37.923 回答