xiaorourou945
2016-02-27 08:29
采纳率: 37.8%
浏览 1.8k
已采纳

怎么把主线程getText()得到的String放进子线程

图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

9条回答 默认 最新

  • 随我逐流 2016-02-27 14:52
    已采纳

    不一定非要定义全局变量,我修改的如下,你运行看看,不行再定义全局变量。修改了3个地方
    public class StartLoginActivity extends Activity {

    // 账号输入框
    private EditText inputAccount;

    //密码输入框
    private EditText inputPassword;

    // 登录按钮
    private Button loginButton;

    //private GlobalVariable mGlobalVariable;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    //透明状态栏
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    //透明导航栏
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    
    inputAccount = (EditText) findViewById(R.id.account_edittext);
    inputPassword = (EditText) findViewById(R.id.password_edittext);
        //1.
        **final String strInputAccount =  inputAccount.getText().toString();
        final String strInputPassword = inputPassword.getText().toString();**
    
    Button loginButton = (Button) findViewById(R.id.login_button);
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View source) {
            //2.
                        //new GeocodeingTask().execute( mGlobalVariable.account = inputAccount.getText().toString(), mGlobalVariable.password = inputPassword.getText().toString());
                        new GeocodeingTask().execute(strInputAccount , strInputPassword);
        }
    });
    

    }

    public class GeocodeingTask extends AsyncTask> {

    @Override
    protected List<GlobalVariable> doInBackground(String... params) {
    

    //3.这里不定参数params是异步任务传入的参数
    //params[0]对应的是传入的strInputAccount,parmas[1]对应的是传入的strInputPassword

    String json = RequestUtil.RequestJson("http://wechat.svner.com/login.php", "POST", params[1]);
    Gson gson = new Gson();
    class tmp {
    public int result;
    }
    System.out.println(json);
    tmp n = gson.fromJson(json, tmp.class);
    if (n.result == 0) {
    Toast.makeText(getApplicationContext(), "账号或密码错误", Toast.LENGTH_SHORT).show();
    System.out.print("error============================");
    } else {
    Intent intent = new Intent(StartLoginActivity.this, StartMainBodyActivity.class);
    startActivity(intent);
    }
    return null;
    }
    }
    }

    、、、
    、、、、
    、、、、、

    public class GlobalVariable {
    public String account;
    public String password;
    }

    点赞 打赏 评论
  • 侧耳_倾听 2016-02-27 08:42

    用全局变量。。。在外面定义String

    点赞 打赏 评论
  • 随我逐流 2016-02-27 09:44

    在new GeocodeingTask().execute(password)中传入,记得把password 加final修饰。
    然后在doInBackGround(Integer... params)中 修改成oInBackGround(String... params)再把你现在的password引用改成params[0]就好了

    点赞 打赏 评论
  • oyljerry 2016-02-27 11:21

    通过全局变量或者线程通信的方式传递

    点赞 打赏 评论
  • Antway_ 2016-02-27 12:36

    定义一个全局变量或者作为参数传递给你的异步任务

    点赞 打赏 评论
  • xiaorourou945 2016-02-27 13:00

    public class StartLoginActivity extends Activity {

    // 账号输入框
    private EditText inputAccount;
    
    //密码输入框
    private EditText inputPassword;
    
    // 登录按钮
    private Button loginButton;
    
    private GlobalVariable mGlobalVariable;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
    
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    
        inputAccount = (EditText) findViewById(R.id.account_edittext);
        inputPassword = (EditText) findViewById(R.id.password_edittext);
    
        Button loginButton = (Button) findViewById(R.id.login_button);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View source) {
                new GeocodeingTask().execute( mGlobalVariable.account = inputAccount.getText().toString(), mGlobalVariable.password = inputPassword.getText().toString());
            }
        });
    }
    
    public class GeocodeingTask extends AsyncTask<String, Void, List<GlobalVariable>> {
    
        @Override
        protected List<GlobalVariable> doInBackground(String... params) {
    
            String json = RequestUtil.RequestJson("http://wechat.svner.com/login.php", "POST", params[0]);
            Gson gson = new Gson();
            class tmp {
                public int result;
            }
            System.out.println(json);
            tmp n = gson.fromJson(json, tmp.class);
            if (n.result == 0) {
                Toast.makeText(getApplicationContext(), "账号或密码错误", Toast.LENGTH_SHORT).show();
                System.out.print("error============================");
            } else {
                Intent intent = new Intent(StartLoginActivity.this, StartMainBodyActivity.class);
                startActivity(intent);
            }
            return null;
        }
    }
    

    }

    、、、
    、、、、
    、、、、、

    public class GlobalVariable {
    public String account;
    public String password;
    }

    点赞 打赏 评论
  • xiaorourou945 2016-02-27 13:09

    图片说明

    点赞 打赏 评论
  • xiaorourou945 2016-02-27 15:17

    图片说明

    点赞 打赏 评论
  • 东风玖哥 2016-02-29 00:34

    loop或者handler

    点赞 打赏 评论

相关推荐 更多相似问题