从android中的另一个类继承值

时间:2014-04-24 11:21:00

标签: android android-intent

我想保留用户在编辑文本中输入的值。 然后我想在另一个类中使用输入的值。基本上我希望用户在这个类中输入他们的用户名

public class Loginpage extends Activity implements OnClickListener{

    EditText usern,passw,dis;
    Button ulogin;
    String k;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginpage);
        usern=(EditText) findViewById(R.id.usern);
        passw=(EditText) findViewById(R.id.passw);
        dis=(EditText) findViewById(R.id.dis);
        ulogin=(Button) findViewById(R.id.ulogin);
        ulogin.setOnClickListener(this);
        k=usern.getText().toString();
    }
    @Override
    public void onClick(View p) {
        // TODO Auto-generated method stub
        switch(p.getId()){
        case R.id.ulogin:
            k=usern.getText().toString();
            String passwd=passw.getText().toString();
            Process cat = new Process(this);
            cat.open();
            String returnpass = cat.getpass(k);
            cat.close();
            if(passwd.equals(returnpass))
            {
                Intent i=new Intent("com.example.billpay.USEROPTION");
                startActivity(i);
                Dialog r=new Dialog(this);
                r.setTitle("Login Successfull");
                TextView wg= new TextView(this);
                wg.setText("Success");
                r.setContentView(wg);
                r.show();

            }
            else
            {
                Dialog r=new Dialog(this);
                r.setTitle("Wrong Username or Password");
                TextView po= new TextView(this);
                po.setText("Failure");
                r.setContentView(po);
                r.show();
            }


        }

    }


}

然后我想使用他们在另一个类的编辑文本中输入的值。我想在此页面中显示他们的用户名。

 public class Userprofile extends Loginpage {


    TextView pra;
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.userprofile);
        pra=(TextView)findViewById(R.id.pra);
        EditText p=usern;
        String op=p.getText().toString();
        pra.setText(k);

    }

}

但我无法显示其价值

3 个答案:

答案 0 :(得分:0)

只需使用 - Shared Preference

即可

存储值并在其他类中检索值

答案 1 :(得分:0)

请使用-Shared Preference:

这是一个简单的例子:

link

答案 2 :(得分:0)

在第一堂课:

Context c=getApplicationContext();
String username;

使用您的用户名获得String:

Intent intent=new Intent(c, YourSecondClass.class);
intent.putExtra("username", username);
startActivity(intent);

在你的第二堂课:

String username;
Bundle bundle=new Bundle();
bundle=getIntent.getExtras();
username=bundle.getString("username");

编辑:

如果没有直接从frist调用第二个类,则应使用SharedPreferences:

首先,创建一个名为" shared_prefs"的文件夹。在项目的根目录中。在该文件夹中,使用您想要的名称创建一个xml文件。在xml里面:

<map>
<string name="mail">user@mail.com</string>
<tring name="other">othervariable</tring>
</map>

在代码中,要达到该变量,请执行以下操作:

public class MainActivity extends Activity {

TextView mail;
Context c;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    c=getApplicationContext();

    //Putting values in preferences.xml
    SharedPreferences prefs =
             getSharedPreferences("preferences",Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();     
    editor.putString("mail", "correo@mail.com");
    editor.commit();

            //Getting things from preferences.xml
    String correo=prefs.getString("mail", null);
    mail=(TextView)findViewById(R.id.mail);
    mail.setText(correo);

}



}
相关问题