a = b
プロパティファイルで使用されている形式に満足している限り、 99%の方法で目標を達成できます。
Properties properties = new Properties();
try {
properties.load(new FileInputStream(filename));
} catch (IOException e) {
// the file is missing or is not in 'a = b' format
}
key
は、ユーザーから文字列を含む変数を取得したため、ファイルに行が含まれている場合"a"
、の結果properties.getProperty ( key )
は等しくなります。ファイルからマップをロードし、エスケープと文字エンコードの問題をすべて処理するには、C++でそれ以上のものが必要になると確信しています。"b"
a = b
プロパティが、ユーザーのファイルシステムではなく、Androidプロジェクトのアセットフォルダーにあるmappings.propertiesというテキストファイルに保持されている場合は、次のようになります。
final AssetManager am = getResources().getAssets();
final Properties properties = new Properties();
try {
properties.load( am.open("mappings.properties"));
} catch (IOException e) {
// the file is missing or is not in 'a = b' format
}
この次のビットは、編集ボックスに「a」が入力されている場合に「b」が含まれるトーストメッセージを表示するためにAndroidチュートリアルから借用されています。XMLファイルを使用してGUIを設定し、Javaでリスナーを追加することは、他の言語と比較してかなり冗長であるため、ここから行数を取得できます。これは、仮想マシンではなく、JavaおよびXML構文によるものです。
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
Toast.makeText(YourOuterViewClass.this,
properties.getProperty(edittext.getText().toString()),
Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});