簡體   English   中英

單擊按鈕即可恢復相同的活動

[英]resuming the same activity on button click

我正在制作一個用戶需要在其中添加信息的android應用。 我想要如果用戶留下任何空白字段並單擊按鈕,則應啟動填充其他字段的相同活動。

我為此使用了意圖,並且能夠再次開始活動,但是所有字段都被清空。 我應該如何做到這一點。

謝謝

  public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.register);


    Register=(ImageButton)findViewById(R.id.register);
    Reset=(ImageButton)findViewById(R.id.reset);

    firstName=(EditText)findViewById(R.id.firstName);
    surName=(EditText)findViewById(R.id.surname);
    email=(EditText)findViewById(R.id.eMAil);
    password=(EditText)findViewById(R.id.passWord);
    retypePassword=(EditText)findViewById(R.id.retypePassword);
    town=(EditText)findViewById(R.id.town);
    cellno=(EditText)findViewById(R.id.cellno);
    dateOfBirth=(Button)findViewById(R.id.datepickerbutton);         
 Register.setOnClickListener(clickRegisterListener);

  private OnClickListener clickRegisterListener = new OnClickListener() {
    public void onClick(View v)
    {
         try{
                FirstName=firstName.getText().toString();
                SurName=surName.getText().toString();
                sex.setOnItemSelectedListener(new            MyOnItemSelectedListener());
                Email=email.getText().toString();
                Password=password.getText().toString();
                RetypePassword=retypePassword.getText().toString();
                Town=town.getText().toString();
                Cellno=cellno.getText().toString();

                if(FirstName.equals(""))
                {
                    AlertDialog.Builder alertbox = new AlertDialog.Builder(Register.this);
                    alertbox.setMessage("First Name field can not be left empty!");
                    alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface arg0, int arg1)
                        {

                        Intent intent = new Intent(Register.this, Register.class);
                       //   onSaveInstanceState(bundle);
                        startActivity(intent);
                        }
                      });
                    alertbox.show();
                }

您需要做的是將您想要顯示的所有信息都傳遞給活動。 創建意圖時,您會在意圖中“放入額外的”信息。

Intent i = new Intent(this, OtherActivity.class);
i.putExtra("variableName", "variable value");

然后在活動中,您可以檢索信息:

Bundle extras = this.getIntent().getExtras(); 
String var = extras.getString("variableName");

編輯

如果要在暫停狀態下保存活動狀態,則需要將感興趣的變量保存在onPause()方法中,然后在onResume()方法中進行恢復。

您應該使用startActivityForResult()

暫無
暫無

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

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