簡體   English   中英

從android中的另一個類繼承值

[英]inherit value from another class in android

我想保留用戶在“編輯”文本中輸入的值。 然后我想在另一個類中使用該輸入的值。基本上我希望用戶在此類中輸入其用戶名

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);

    }

}

但是我無法展示他們的價值

只需使用- 共享首選項

在其他類中存儲價值並獲取價值

請使用-共享首選項:

這是一個簡單的示例,該如何做:

鏈接

在頭等艙:

Context c=getApplicationContext();
String username;

將字符串和用戶名一起使用后:

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");

編輯:

如果不是直接從第二個類中直接調用第二個類,則應使用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);

}



}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM