- 文字コード
- File -> Settings -> Edor -> File Encoding -> Project Encoding < ここをUTF-8にする
- Default Encoding < こっちもUTF-8にして右のチェックを入れておく
- SDK version
- File -> Project Structure -> Flavorsタブ
- うまく動かなかったときなど、minimum SDK versionなどを変更する
2015年11月10日火曜日
Android Studioの設定(文字コード・SDK version)
Android Studioでよく変更する部分の覚書
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();
}
}
2015年2月3日火曜日
C#でExcel参照
ドラッグドロップするとシート1の(1,1)のセルの内容を表示してみる
あと、worksheetの名前をtextbox2の内容で更新してみる
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;
namespace excelTest01
{
public partial class Form1 : Form
{
Microsoft.Office.Interop.Excel.Application ExcelApp;
Workbook Workbook;
Worksheets Worksheets;
Worksheet Worksheet;
Range Range;
public Form1()
{
InitializeComponent();
ExcelApp = new Microsoft.Office.Interop.Excel.Application();
}
private void button1_Click(object sender, EventArgs e)
{
}
/// <summary>
/// ドロップ時イベント
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_DragDrop(object sender, DragEventArgs e)
{
string[] filelist = (string[])e.Data.GetData(DataFormats.FileDrop);
textBox1.Text = Path.GetFileName(filelist[0]);
string file = Path.GetFileName(filelist[0]);
// excelを開く
Workbook = ExcelApp.Workbooks.Open(filelist[0]);
// Worksheet = Workbook.Sheets[1]; // ワークシートのインデックスは1から
Worksheet = Workbook.Sheets[2]; // ワークシートのインデックスは1から
Range = Worksheet.Cells[1, 1];
textBox1.Text = Range.Text;
Worksheet.Name = textBox2.Text; // ワークシートのnameプロパティでワークシート名を変更出来るようだ
// excelを閉じる
ExcelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp);
}
/// <summary>
/// ドラッグエンター時イベント
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
}
}
あと、worksheetの名前をtextbox2の内容で更新してみる
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;
namespace excelTest01
{
public partial class Form1 : Form
{
Microsoft.Office.Interop.Excel.Application ExcelApp;
Workbook Workbook;
Worksheets Worksheets;
Worksheet Worksheet;
Range Range;
public Form1()
{
InitializeComponent();
ExcelApp = new Microsoft.Office.Interop.Excel.Application();
}
private void button1_Click(object sender, EventArgs e)
{
}
/// <summary>
/// ドロップ時イベント
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_DragDrop(object sender, DragEventArgs e)
{
string[] filelist = (string[])e.Data.GetData(DataFormats.FileDrop);
textBox1.Text = Path.GetFileName(filelist[0]);
string file = Path.GetFileName(filelist[0]);
// excelを開く
Workbook = ExcelApp.Workbooks.Open(filelist[0]);
// Worksheet = Workbook.Sheets[1]; // ワークシートのインデックスは1から
Worksheet = Workbook.Sheets[2]; // ワークシートのインデックスは1から
Range = Worksheet.Cells[1, 1];
textBox1.Text = Range.Text;
Worksheet.Name = textBox2.Text; // ワークシートのnameプロパティでワークシート名を変更出来るようだ
// excelを閉じる
ExcelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp);
}
/// <summary>
/// ドラッグエンター時イベント
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
}
}
2014年8月12日火曜日
Windows8の他のPCのドライブを参照
Windows8.1を使っていますが、エクスプローラで"ネットワーク"を見ていると他のWindows8.1のマシンが見えます
Windows7以前はフォルダ共有をしないとダメだった気がしますが、Windows8(.1?)からはMSアカウントにしていればそのままアクセス出来た気がしましたが...
久々にアクセスしようと思ったら"パスワードが違っています"と言われます(あれ??)
よく見ると、ドメインにローカルPCの名前がついたようなログイン名になっています
ex) computer_name01\hogehoge
みたいな(compter_name01は現在自分がログインしているコンピュータ名)
これではダメですね...(エラーの"パスワードが違っています"だと勘違いしますが...)
という事で適当に
computer_name02\hogehoge@live.jp
としたら入れました(!?)
便利な機能ですが、"パスワードが違う"と言われると勘違いしますよね...
Windows7以前はフォルダ共有をしないとダメだった気がしますが、Windows8(.1?)からはMSアカウントにしていればそのままアクセス出来た気がしましたが...
久々にアクセスしようと思ったら"パスワードが違っています"と言われます(あれ??)
よく見ると、ドメインにローカルPCの名前がついたようなログイン名になっています
ex) computer_name01\hogehoge
みたいな(compter_name01は現在自分がログインしているコンピュータ名)
これではダメですね...(エラーの"パスワードが違っています"だと勘違いしますが...)
という事で適当に
computer_name02\hogehoge@live.jp
としたら入れました(!?)
便利な機能ですが、"パスワードが違う"と言われると勘違いしますよね...
登録:
投稿 (Atom)