我有這個小登錄程序將數據保存到共享首選項。主要活動有「登錄」和「帳戶」按鈕。我想要禁用登錄按鈕時,共享首選項爲空,並啓用用戶的「帳戶」按鈕註冊一個帳戶,這是我的第二活動。如果共享首選項爲空,如何啓用/禁用2個按鈕?如何從另一個活動禁用按鈕
活動1個
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_interface); final Button login = (Button)findViewById(R.id.Login); final Button account = (Button)findViewById(R.id.Account); login.setEnabled(false); login.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { startActivity(new Intent(MainActivity.this, Summation.class)); } }); account.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { startActivity(new Intent(MainActivity.this, Append.class)); } }); Button exit = (Button)findViewById(R.id.Exit); exit.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { finish(); System.exit(0); } });
活動2
protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); Stetho.initializeWithDefaults(this); setContentView(R.layout.append_interface); final EditText usernameAppend = (EditText)findViewById(R.id.usernameAppend); final EditText passwordAppend = (EditText)findViewById(R.id.passwordAppend); //this buttons appends the username and password to the SharedPreferences Button append = (Button)findViewById(R.id.Append); append.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { final SharedPreferences mySharedPref = getSharedPreferences(PREF_NAME, MODE_PRIVATE); final SharedPreferences.Editor editor = mySharedPref.edit(); //Append get data final String un = usernameAppend.getText().toString(); editor.putString(editorUsername,un); editor.commit(); final String pw = passwordAppend.getText().toString(); editor.putString(editorPassword, pw); editor.commit(); if(mySharedPref != null){ Toast.makeText(Append.this,"Account saved",Toast.LENGTH_LONG).show(); startActivity(new Intent(Append.this, MainActivity.class)); }else{ Toast.makeText(Append.this,"Please enter again",Toast.LENGTH_LONG).show(); } } } ); //Exit button,, exits the app Button exit = (Button)findViewById(R.id.Exit); exit.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { finish(); System.exit(0); } }); }
不清楚,你能指定一個執行級別嗎? – sansa
主要活動有3個按鈕;登錄,帳戶,退出。帳戶按鈕爲用戶提供註冊活動,將用戶名和密碼附加到共享首選項中。如果用戶名和密碼已添加到共享首選項中,登錄按鈕將啓用。如果共享首選項爲空,則登錄按鈕被禁用。感謝和我的共享首選項被宣佈在我的活動2擴展活動1 –