0

在这里,我想在 myclass.java 中发送“我们的密码”的值,那么我该怎么做呢?

public class SetPassword extends Activity{  
@Override  
protected void onCreate(Bundle savedInstanceState) {  
// TODO Auto-generated method stub  
super.onCreate(savedInstanceState);  
setContentView(R.layout.set_password);

    final EditText ed=(EditText)findViewById(R.id.setpass);
    Button submit=(Button)findViewById(R.id.button1);

    submit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
        String  ourpassword=ed.getText().toString();

        }
    });
}
4

3 回答 3

0
Create object of this class and get the value as

Define string as static
Object.String;

您也可以使用使用数据库。这是存储和检索密码的更好方法

于 2013-03-29T11:42:10.747 回答
0

如果 myclass.java 是另一个 Activity 类,您可以将密码设置为 Intent 中的参数。例如:

Intent intent = new Intent(getApplicationContext(),myclass.class);
intent.putExtra("password", ourpassword);
startActivity(intent);

...然后您可以从 myclass.java 中的 onCreate() 方法中获取值,如下所示:

String password = "";
if (getIntent().getStringExtra("password") != null) password = getIntent().getStringExtra("password");

编辑:您将 Intent 代码放在提交按钮的 onClick() 方法中。

于 2013-03-29T11:49:47.927 回答
0

要在活动之间传递数据,请使用意图。

   Intent i= new Intent("com.example.myclass");
   i.puExtra("mypassword",ourpassword);
   startActivtiy(i);   

在我的班级

  Bundle extras = getIntent().getExtras();
 if (extras != null) {
 String password = extras.getString("mypassword");
}

您还可以通过将值传递给类的构造函数来将值传递给另一个类

   myClass mc= new myClass(ourPassword);
   mc.doSomething(); //call some method in another class     

在你的课堂上

   class myClass{
       String pwd;
        public myclass(String password)
        {
               pwd =password;

        } 
        publidc void doSomething()
        {

        }
    }  

假设您想将值传递给 asynctask 之类的类

 //pass value as a parameter to the class constructor
MyAsyncTask my= new MyAycTask(ourpassword).execute();

在我的异步任务中

class MyAsyncTask extends AsyncTask<Void,Void,Void>
{
       public M(String password)//receive value here
       {

       }
    other methods....
}
于 2013-03-29T11:50:16.757 回答