androidプログラミング
2018 Jan. 06.
2018 Jan. 04.
2018 Jan. 03.
2017 Nov. 23.
2017 Aug. 16.
android, プログラミング, emulator, context, 1行, 読み込み,SD, ファイル読み込み, AlertDialog
preferenceで配列を扱う
https://qiita.com/ueno-yuhei/items/d51c00c5be3971f4cd40
コマンドラインからのemulator起動
$ Android/Sdk/tools/emulator -avd EMULATOR -qemu -m 512 -enable-kvm
EMULATOR: advの名前
context
参考サイト
http://yuki312.blogspot.jp/2012/02/thisgetapplicationcontextactivityapplic.html
1行読み込み
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
if (br != null) try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
String str;
try {
while((str = br.readLine()) != null){
Log.v("Read",str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
SDカード上のファイルの読み込み
private String filename = "hoge.txt";
private File directory = Environment.getExternalStorageDirectory();
private String filepath = directory.getAbsolutePath() + "/YourDir/" + filename;
private File file = new File(filepath);
// SDカードマウント状態
final String state = Environment.getExternalStorageState();
if (!state.equals(Environment.MEDIA_MOUNTED)) {
try {
throw new IOException("No Mount");
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
if (br != null) try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
String str;
try {
while((str = br.readLine()) != null){
Log.v("Read",str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
AlertDialog setItems()利用時のコードと挙動
サンプル1 ダイアログにボタンを置かず、アイテムをクリックすれば前画面に戻る
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn = (Button)findViewById(R.id.btn);
btn.setText("Show Dialog");
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final CharSequence[] items = {"car", "train", "byke"};
new AlertDialog.Builder(MainActivity.this)
.setTitle("Select tool")
.setIcon(R.drawable.myIcon)
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int which) {
btn.setText(String.format("%sが選択されました。",items[which]));
}
})
.show();
}
});
}
}
サンプル2 ダイアログにボタンを置かず、アイテムをクリックしても前画面に戻らない
public class MainActivity extends Activity implements DialogInterface.OnClickListener {
final String[] items = {"car", "train", "byke"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn = (Button)findViewById(R.id.btn);
btn.setText("Show Dialog");
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myShowDialog("Not Selected");
}
});
}
@Override
public void onClick(DialogInterface dlg, int which) {
myShowDialog(items[which]);
}
public void myShowDialog(final String titleStr) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(titleStr);
dialog.setIcon(R.drawable.myIcon);
dialog.setItems(items, this);
dialog.show();
}
}
サンプル3 ダイアログにボタンを置くが、アイテムのクリックでは前画面に戻らない
public class MainActivity extends Activity implements DialogInterface.OnClickListener {
TextView tv;
final String[] items = {"car", "train", "byke"};
final List listItems = Arrays.asList(items); //配列をList型オブジェクトに変換
@Override
public void onClick(DialogInterface dlg, int which) {
Log.v("dialog_which", String.valueOf(which)); // int of order of items
showDialog(items[which]);
}
public void showDialog(final String titleStr) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(titleStr);
dialog.setIcon(R.drawable.myIcon);
dialog.setItems(items, this);
dialog.setPositiveButton("決 定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int index) {
Log.v("dialog_index", String.valueOf(index)); // unexpected int
tv.setText(titleStr);
}
});
dialog.setNeutralButton("前 へ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int index) {
Log.v("dialog_index", String.valueOf(index)); // unexpected int
int i = listItems.indexOf(titleStr); //list内に引数の要素が存在する最小のインデックスを返す
int numArray = (i+3-1)%3;
tv.setText(items[numArray]);
}
});
dialog.setNegativeButton("2つ前へ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int index) {
Log.v("dialog_index", String.valueOf(index)); // unexpected int
int i = listItems.indexOf(titleStr); //list内に引数の要素が存在する最小のインデックスを返す
int numArray = (i+3-2)%3;
tv.setText(items[numArray]);
}
});
dialog.show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = ((TextView)findViewById(R.id.tv));
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
showDialog("");
}
});
}
}