Android範例程式大全
以下是一些 Android 應用程式的示例代碼大全:
1. 簡單 Android 應用程式:這個示例應用程式是一個簡單的 Android 應用程式,用於顯示天氣信息和城市名稱。它使用了一些基本的技術,例如 Android 布局和互動。
```java
public class SimpleWeatherAppActivity extends AppCompatActivity {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = findViewById(R.id.textview);
WeatherData weatherData = new WeatherData();
String cityName = weatherData.getCityName();
String weatherInfo = weatherData.getWeatherInfo();
mTextView.setText(cityName + ": " + weatherInfo);
}
}
```
2. 登錄應用程式:這個示例應用程式是一個簡單的登錄應用程式,它使用 SQLite 資料庫來存儲用戶信息。它包括一個登錄表單和一個用戶註冊表單。
```java
public class LoginActivity extends AppCompatActivity {
private SQLiteDatabase db;
private EditText usernameEditText;
private EditText passwordEditText;
private Button loginButton;
private SharedPreferences sharedPreferences;
private String[] users = new String[]{"user1", "user2", "user3"};
private boolean isUserLoggedIn = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
sharedPreferences = getSharedPreferences("login_info", MODE_PRIVATE);
usernameEditText = findViewById(R.id.usernameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
loginButton = findViewById(R.id.loginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
if (sharedPreferences.contains("username") && sharedPreferences.contains("password")) {
String savedUsername = sharedPreferences.getString("username", "");
String savedPassword = sharedPreferences.getString("password", "");
if (savedUsername.equals(username) && savedPassword.equals(password)) {
Toast.makeText(LoginActivity.this, "Welcome back!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(LoginActivity.this, "Invalid username or password!", Toast.LENGTH_SHORT).show();
}
} else {
db = openOrCreateDatabase("login_db", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS users (" + "id INTEGER PRIMARY KEY AUTOINCREMENT," + "username TEXT," + "password TEXT)");
db.execSQL("INSERT INTO users (username, password) VALUES ('" + username + "', '" + password + "')");
sharedPreferences.edit().putString("username", username).apply();
sharedPreferences.edit().putString("password", password).apply();
Toast.makeText(LoginActivity.this, "Registered successfully!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
```
以上代碼僅為示例,僅供參考。在編寫自己的 Android 應用程式時,需要根據實際情況進行調整和修改。同時,還有很多其他類型的 Android 應用程式示例代碼,可以在 Android 開源項目、部落格、教程等地方找到。
以上就是【Android範例程式大全】的相關內容,敬請閱讀。