解説サイトを参考にしてinstallしました。
- installはadminで実施
- 起動は一般ユーザ
で大丈夫の模様
haxがinstallされなかったようなのでSDK managerから実施
初回のエミュレータ起動時に"haxがinstallされていない"と出たのでメッセージ下のinstallをクリックすると"install done"と表示される(のに相変わらずinstallされていない表示)
そのまま進めるとlogでは"hax enable"と出ているので大丈夫の模様
defaultの状態ではSDKは最新版(mashmallow対応)のようだが、minimum SDKではkitkat等も指定できるので、とりあえず変更せずに様子見する事にする
emulater imageはmashmallowで入っているが、"Nexus Sでmashmallow"なVirtual Deviceを作成してとりあえずビルド -> 実行できる事が確認できている
(DLに時間がかかるので様子見してからKitKatやlollipopのemulaterのemulaterを入れようかなと...)
とりあえずしばらくこれで使ってみます。
2016年4月20日水曜日
2016年2月21日日曜日
android studioのprojectでビルド対象versionの変更
いざエミュレータで試そうとすると、"Older versionだよ"と怒られた時の対処方法
1. Projectのペインを表示する
2. "app"を右クリックし、"Open Module Setting"を選択
3. "Compile Sdk Version"を適当なものに変更する
1. Projectのペインを表示する
2. "app"を右クリックし、"Open Module Setting"を選択
3. "Compile Sdk Version"を適当なものに変更する
2016年2月20日土曜日
ボタン押下時のハンドラを書く(その2)
よく書籍に乗っていたの(そんでもって前に書いたもの)は、
1. デザイナでボタンを配置する(この時のIDを覚えておく)
2. ハンドラの関数を書く。
3.onCreateでボタンをfindViewByIdして、ハンドラとして登録する。
ですが、ボタンの場合のもうちょっと簡単に書く方法
1. デザイナでボタンを配置する
2. ハンドラの関数をMainAcitivyjavaに書く (MainActivityのメソッドとして書く)
public void onButtonClick(View v){
Toast.makeText(MainActivity.this, "クリックした",Toast.LENGTH_SHORT).show();
}
3. デザイナで配置したボタンを選択し、 onClickに"2"で記載したハンドラ名を選ぶ
こっちの方が簡単ですね。
1. デザイナでボタンを配置する(この時のIDを覚えておく)
2. ハンドラの関数を書く。
3.onCreateでボタンをfindViewByIdして、ハンドラとして登録する。
ですが、ボタンの場合のもうちょっと簡単に書く方法
1. デザイナでボタンを配置する
2. ハンドラの関数をMainAcitivyjavaに書く (MainActivityのメソッドとして書く)
public void onButtonClick(View v){
Toast.makeText(MainActivity.this, "クリックした",Toast.LENGTH_SHORT).show();
}
3. デザイナで配置したボタンを選択し、 onClickに"2"で記載したハンドラ名を選ぶ
こっちの方が簡単ですね。
2016年2月4日木曜日
レイアウトファイルを書き始める
XMLファイルを書き始めはこんな感じでやるみたいだ。
1. ペインをProjectにする
2. res/layoutを右クリック
3. 新規 -> layout resource file を選択
入力用のダイアログが表示されるので以下入力
4. "File name"にファイル名(につながる)を入れる
先頭大文字じゃなくてもいいみたい...
というか、大文字使えないみたいなので、アンダースコアとかにするみたいだ
5. "Root element"に配置用のタグ(ListViewとかLinearLayoutとか)を記載する
文字を入れていると補間してくれるので適当に ...
6. OKボタン押下
デザイナに表示されるので、"デザイン" or "テキスト"に切り替えて表示を調整する
(Projectペインの"layout"に追加されているので選んで修正)
1. ペインをProjectにする
2. res/layoutを右クリック
3. 新規 -> layout resource file を選択
入力用のダイアログが表示されるので以下入力
4. "File name"にファイル名(につながる)を入れる
先頭大文字じゃなくてもいいみたい...
というか、大文字使えないみたいなので、アンダースコアとかにするみたいだ
5. "Root element"に配置用のタグ(ListViewとかLinearLayoutとか)を記載する
文字を入れていると補間してくれるので適当に ...
6. OKボタン押下
デザイナに表示されるので、"デザイン" or "テキスト"に切り替えて表示を調整する
(Projectペインの"layout"に追加されているので選んで修正)
2016年1月25日月曜日
画像リソースをstaticに使用する
ちょっと参考にしている情報と違っていた為...Android studioのversionの違いかもしれないけど(Betaのままだし)
1. explorerで適当な画像(今回は493x357でPNGにした画像)をCtrl-Cでコピー
ファイル名はすべて小文字、拡張子も小文字
2. Android studioのProjectペインでres/drawableのを左クリックしてCtrl-Vでペーストする
3. dialogが出るのでそのままOKとする
今回は"mdpi"が選択されているがその状態で良いようだ
ここで"drawable"を選択した場合に実行時に "INSTALL_FAILED_OLDER_SDK" エラーが発生
(対応version云々と思わしきエラーも出ていたが、上記操作をすれば出なかった)
4. ImageViewにstaticに使用する場合はImageViewをダブルクリックして"..."の選択で↑で
追加したリソースを選択する
1. explorerで適当な画像(今回は493x357でPNGにした画像)をCtrl-Cでコピー
ファイル名はすべて小文字、拡張子も小文字
2. Android studioのProjectペインでres/drawableのを左クリックしてCtrl-Vでペーストする
3. dialogが出るのでそのままOKとする
今回は"mdpi"が選択されているがその状態で良いようだ
ここで"drawable"を選択した場合に実行時に "INSTALL_FAILED_OLDER_SDK" エラーが発生
(対応version云々と思わしきエラーも出ていたが、上記操作をすれば出なかった)
4. ImageViewにstaticに使用する場合はImageViewをダブルクリックして"..."の選択で↑で
追加したリソースを選択する
2015年11月10日火曜日
Android Studioの設定(文字コード・SDK version)
Android Studioでよく変更する部分の覚書
- 文字コード
- File -> Settings -> Edor -> File Encoding -> Project Encoding < ここをUTF-8にする
- Default Encoding < こっちもUTF-8にして右のチェックを入れておく
- SDK version
- File -> Project Structure -> Flavorsタブ
- うまく動かなかったときなど、minimum SDK versionなどを変更する
2015年10月19日月曜日
background処理を書いてみる
調べてみると、以下のように作るのが一般的?のようだ
1. IntentServiceを継承したクラスを記載
runnableのrunをOverrideし、重い処理を書く
IntentServiceのコンストラクタでHandlerをnewする
IntentServiceのonHandleIntentでhandlerをpostする(これによってUIスレッド側で処理を実施)
一定時間後に繰り返したい場合はpostDelayedで遅延実行
(ThreadSleepを使っても同じようだ)
2. MainActivityで上記Intentを作成、開始 startService で開始
データのやりとりとしては
MainActivity -> IntentService
開始時(startServiceする前)にsetActionで文字列を渡す
IntentService -> MainActivity
MainActivityでBroadcastReceiverを登録しておく
Intentを作成し、putExtraで文字列を登録し、sendBroadcastで送信
---
public class MainActivity extends Activity {
Button buttonStart;
static public TextView textCount;
Intent intent;
boolean isRunning = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonStart = (Button)findViewById(R.id.button_start);
textCount = (TextView)findViewById(R.id.text_count);
// ボタン押下時のハンドラ
buttonStart.setOnClickListener(new ButtonClickListener_startButton()); // 実体は↓のクラス
// Broadcastを受けとるReceiverを設定
DataReceiver dataReceiver = new DataReceiver();
// LocalBroadcastの設定
IntentFilter intentFilter = new IntentFilter("BkTimerEvent");
LocalBroadcastManager.getInstance(this).registerReceiver(dataReceiver, intentFilter);
}
// ボタン押下ハンドラとして登録される
private class ButtonClickListener_startButton implements View.OnClickListener {
@Override
public void onClick(View v){
Log.d("BackgroundTimer", "onClick"); // ログ出力
intent = new Intent(getApplication(), BackgroundIntentTimer.class); // intentを生成
intent.setAction("start"); // 文字列を受け渡す。受け側はonHandleIntentの引数から、getActionで文字列を取得する事ができる(putExtraでも同様の事が出来そう)
startService(intent); // 開始。onHandleIntentが呼び出され
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
// Broadcastを受けとる
public class DataReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent){
Log.d("BackgroundTimer", "MainActivity Received");
// Broadcastされたメッセージを取り出す
String message = intent.getStringExtra("Message");
if(message!=null) {
textCount.setText(message);
}
}
}
}
---
package com.example.training.backgroundtimer;
import android.app.IntentService;
import android.content.Intent;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
/**
* Timer
*/
public class BackgroundIntentTimer extends IntentService{
private Handler handler;
public BackgroundIntentTimer(){
super("com.example.training.backgroundtimer.BackgroundIntentTimer");
handler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("BackgroundTimer", "onHandleIntent");
String action = intent.getAction(); // Activity側でsetActionした内容を出力
// callbackをremoveしないと重複処理される為
handler.removeCallbacks(thread);
if(action.equals("start")){
count = 0;
handler.post(thread); // UIスレッドに対して処理をポストする(描画処理の為)
}
Log.d("BackgroundTimer", action);
}
// スレッド
private Runnable thread = new Runnable() {
@Override
public void run() {
// ここに重い処理を呼び出す
Log.d("BackgroundTimer", "run!!");
timerProcess();
}
};
private void timerProcess(){
// 表示更新用にメッセージ(Intent)送信
Log.d("BackgrountTimer", "timer Process"); Intent messageIntent = new Intent("BkTimerEvent");
messageIntent.putExtra("Message", Integer.toString(count));
LocalBroadcastManager.getInstance(this).sendBroadcast((messageIntent));
// 1秒ごとにカウントアップする処理
/*
try {
LocalBroadcastManager.getInstance(this).sendBroadcast((messageIntent)); handler.removeCallbacks(thread);
Thread.sleep(1000);
} catch (InterruptedException ie){
Log.d("BackgroundTimer", "timer error");
}
*/
handler.removeCallbacks(thread); handler.postDelayed(thread,1000); // 遅延実行する場合
// handler.post(thread); //Thread.sleepする場合
}
}
}
1. IntentServiceを継承したクラスを記載
runnableのrunをOverrideし、重い処理を書く
IntentServiceのコンストラクタでHandlerをnewする
IntentServiceのonHandleIntentでhandlerをpostする(これによってUIスレッド側で処理を実施)
一定時間後に繰り返したい場合はpostDelayedで遅延実行
(ThreadSleepを使っても同じようだ)
2. MainActivityで上記Intentを作成、開始 startService で開始
データのやりとりとしては
MainActivity -> IntentService
開始時(startServiceする前)にsetActionで文字列を渡す
IntentService -> MainActivity
MainActivityでBroadcastReceiverを登録しておく
Intentを作成し、putExtraで文字列を登録し、sendBroadcastで送信
---
public class MainActivity extends Activity {
Button buttonStart;
static public TextView textCount;
Intent intent;
boolean isRunning = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonStart = (Button)findViewById(R.id.button_start);
textCount = (TextView)findViewById(R.id.text_count);
// ボタン押下時のハンドラ
buttonStart.setOnClickListener(new ButtonClickListener_startButton()); // 実体は↓のクラス
// Broadcastを受けとるReceiverを設定
DataReceiver dataReceiver = new DataReceiver();
// LocalBroadcastの設定
IntentFilter intentFilter = new IntentFilter("BkTimerEvent");
LocalBroadcastManager.getInstance(this).registerReceiver(dataReceiver, intentFilter);
}
// ボタン押下ハンドラとして登録される
private class ButtonClickListener_startButton implements View.OnClickListener {
@Override
public void onClick(View v){
Log.d("BackgroundTimer", "onClick"); // ログ出力
intent = new Intent(getApplication(), BackgroundIntentTimer.class); // intentを生成
intent.setAction("start"); // 文字列を受け渡す。受け側はonHandleIntentの引数から、getActionで文字列を取得する事ができる(putExtraでも同様の事が出来そう)
startService(intent); // 開始。onHandleIntentが呼び出され
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
// Broadcastを受けとる
public class DataReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent){
Log.d("BackgroundTimer", "MainActivity Received");
// Broadcastされたメッセージを取り出す
String message = intent.getStringExtra("Message");
if(message!=null) {
textCount.setText(message);
}
}
}
}
---
package com.example.training.backgroundtimer;
import android.app.IntentService;
import android.content.Intent;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
/**
* Timer
*/
public class BackgroundIntentTimer extends IntentService{
private Handler handler;
public BackgroundIntentTimer(){
super("com.example.training.backgroundtimer.BackgroundIntentTimer");
handler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("BackgroundTimer", "onHandleIntent");
String action = intent.getAction(); // Activity側でsetActionした内容を出力
// callbackをremoveしないと重複処理される為
handler.removeCallbacks(thread);
if(action.equals("start")){
count = 0;
handler.post(thread); // UIスレッドに対して処理をポストする(描画処理の為)
}
Log.d("BackgroundTimer", action);
}
// スレッド
private Runnable thread = new Runnable() {
@Override
public void run() {
// ここに重い処理を呼び出す
Log.d("BackgroundTimer", "run!!");
timerProcess();
}
};
private void timerProcess(){
// 表示更新用にメッセージ(Intent)送信
Log.d("BackgrountTimer", "timer Process"); Intent messageIntent = new Intent("BkTimerEvent");
messageIntent.putExtra("Message", Integer.toString(count));
LocalBroadcastManager.getInstance(this).sendBroadcast((messageIntent));
// 1秒ごとにカウントアップする処理
/*
try {
LocalBroadcastManager.getInstance(this).sendBroadcast((messageIntent)); handler.removeCallbacks(thread);
Thread.sleep(1000);
} catch (InterruptedException ie){
Log.d("BackgroundTimer", "timer error");
}
*/
handler.removeCallbacks(thread); handler.postDelayed(thread,1000); // 遅延実行する場合
// handler.post(thread); //Thread.sleepする場合
}
}
}
2015年8月27日木曜日
Radioボタンを使ってみる
XMLの記載
RadioGroupで囲むと排他選択になる
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="@+id/radioButton"
android:layout_alignParentStart="true">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Xxx"
android:id="@+id/radioButton"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yyy"
android:id="@+id/radioButton2"
android:layout_below="@+id/radioButton"
android:layout_alignParentStart="true" />
</RadioGroup>
java側は以下
1. ハンドラの登録
// ラジオボタンハンドラ
RadioButton rbuttonXxx = (RadioButton)findViewById(R.id.radioButton);
rbuttonXxx.setOnClickListener(new RadioButtonXxxClickListener());
2. ハンドラを記載
private class RadioButtonXxxClickListener implements View.OnClickListener {
@Override
public void onClick(View v){
// 処理の内容を書く
}
RadioGroupで囲むと排他選択になる
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="@+id/radioButton"
android:layout_alignParentStart="true">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Xxx"
android:id="@+id/radioButton"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yyy"
android:id="@+id/radioButton2"
android:layout_below="@+id/radioButton"
android:layout_alignParentStart="true" />
</RadioGroup>
java側は以下
1. ハンドラの登録
// ラジオボタンハンドラ
RadioButton rbuttonXxx = (RadioButton)findViewById(R.id.radioButton);
rbuttonXxx.setOnClickListener(new RadioButtonXxxClickListener());
2. ハンドラを記載
private class RadioButtonXxxClickListener implements View.OnClickListener {
@Override
public void onClick(View v){
// 処理の内容を書く
}
2015年8月24日月曜日
TextView/EditTextを参照・設定する
EditBox = EditText
Label = TextView
みたいなものでしょうか...
TextViewに文字を表示する
TextView tv = (TextView) findViewById(R.id.textView);
tv.setText(string);
EditTextに入力されている文字を取得する
EditText edv = (EditText) findViewById(R.id.editText);
String string = edv.getText().toString();
Label = TextView
みたいなものでしょうか...
TextViewに文字を表示する
TextView tv = (TextView) findViewById(R.id.textView);
tv.setText(string);
EditTextに入力されている文字を取得する
EditText edv = (EditText) findViewById(R.id.editText);
String string = edv.getText().toString();
ボタン押下時のイベントハンドラを書く
Androidのイベントハンドラの書き方
今回はボタン押下時のイベント
1. MainActivity.javaのonCreateにハンドラを指定するコードを書く
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new ButtonClickListener()); // 引数にリスナを書く
2. ↑のリスナのクラスを書き、onClickをoverrideする
private class ButtonClickListener implements View.OnClickListener {
@Override
public void onClick(View v){
// ここにアクションを書いておく(例:トーストを表示する)
Toast.makeText(MainActivity.this, "クリックしました", Toast.LENGTH_SHORT).show();
}
}
今回はボタン押下時のイベント
1. MainActivity.javaのonCreateにハンドラを指定するコードを書く
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new ButtonClickListener()); // 引数にリスナを書く
2. ↑のリスナのクラスを書き、onClickをoverrideする
private class ButtonClickListener implements View.OnClickListener {
@Override
public void onClick(View v){
// ここにアクションを書いておく(例:トーストを表示する)
Toast.makeText(MainActivity.this, "クリックしました", Toast.LENGTH_SHORT).show();
}
}
登録:
投稿 (Atom)