memo

ああ無情

CheckActivity

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class CheckActivity extends AppCompatActivity {

@Override
public void onStart() {
//スーパークラスのonStartメソッド呼び出し
super.onStart();

//インテントの付加情報取得
Bundle extra = getIntent().getExtras();

//付加情報から入力データ取得
String name = extra.getString("NAME");
String address = extra.getString("ADDRESS");
String gender = extra.getString("GENDER");
String month = extra.getString("MONTH");
String day = extra.getString("DAY");
String beef = extra.getString("BEEF");
String fish = extra.getString("FISH");

//出力用テキストオブジェクト取得
TextView tvname = (TextView) findViewById(R.id.tv_name);
TextView tvaddress = (TextView) findViewById(R.id.tv_address);
TextView tvgender = (TextView) findViewById(R.id.tv_gender);
TextView tvmonth = (TextView) findViewById(R.id.tv_month);
TextView tvday = (TextView) findViewById(R.id.tv_day);
TextView tvbeef = (TextView) findViewById(R.id.tv_beef);
TextView tvfish = (TextView) findViewById(R.id.tv_fish);

//出力用テキストオブジェクトに入力データ設定
tvname.setText(name);
tvaddress.setText(address);
tvgender.setText(gender);
tvmonth.setText(month);
tvday.setText(day);
tvbeef.setText(beef);
tvfish.setText(fish);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check);
//戻るボタン
Button btback = (Button) this.findViewById(R.id.bt_back);
btback.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(CheckActivity.this, OrderActivity.class);
startActivity(intent);
}
});

//確定ボタン
Button btok = (Button) this.findViewById(R.id.bt_ok);
btok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(CheckActivity.this, ThankYouActivity.class);
startActivity(intent);
}
});
}
}

OrderActivity

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;


public class OrderActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
// ボタンを取得
Button button = (Button)findViewById(R.id.bt_next);
// ボタンにクリックリスナーを登録
button.setOnClickListener(new ButtonClickListener());
}
//クリックリスナー定義
private class ButtonClickListener implements OnClickListener {
//onClickメソッド
public void onClick(View v) {
//名前
EditText name=(EditText)findViewById(R.id.et_name);

//住所
EditText address=(EditText)findViewById(R.id.et_address);

//性別
RadioGroup radio=(RadioGroup)findViewById(R.id.rg_gender);
RadioButton radiobutton=(RadioButton)findViewById(radio.getCheckedRadioButtonId());

//生年月日
Spinner month=(Spinner)findViewById(R.id.sp_month);
Spinner day=(Spinner)findViewById(R.id.sp_day);

//注文商品
CheckBox beef=(CheckBox)findViewById(R.id.cb_beef);
CheckBox fish=(CheckBox)findViewById(R.id.cb_fish);

//注文数量
EditText beefqty=(EditText)findViewById(R.id.et_beef);
EditText fishqty=(EditText)findViewById(R.id.et_fish);

//インテントの生成
Intent intent=new Intent(OrderActivity.this,CheckActivity.class);

//入力データをインテントに設定
intent.putExtra("NAME",name.getText().toString());
intent.putExtra("ADDRESS",address.getText().toString());
intent.putExtra("GENDER",radiobutton.getText().toString());
intent.putExtra("MONTH",month.getSelectedItem().toString());
intent.putExtra("DAY",day.getSelectedItem().toString());
if(beef.isChecked()){
intent.putExtra("BEEF",beefqty.getText().toString());
}
if(fish.isChecked()){
intent.putExtra("FISH",fishqty.getText().toString());
}
//次のアクティビティの起動
startActivity(intent);
}
}
}

テキストボックスの名前をトーストで表示

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;


public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ボタンを取得
Button button = (Button)findViewById(R.id.button1);
// ボタンにクリックリスナーを登録
button.setOnClickListener(new ButtonClickListener());
}
//クリックリスナー定義
private class ButtonClickListener implements OnClickListener{
//onClickメソッド
public void onClick(View v){
//テキストを取得
EditText name=(EditText)findViewById(R.id.et_name);
//トーストで表示
Toast.makeText(MainActivity.this,name.getText(),Toast.LENGTH_LONG).show();
}
}
}

Android Studioの設定とエミュレーターの高速化

androidアプリを作成する上でファッキンeclipseと感じた人はAndroid Studioを試してみるのがいいかも。ただちゃんと設定しないとファッキンAndroid Studioになるので注意。eclipseで環境構築するよりだいぶ楽なはず...

 

以下手順(ver 1.5.1)

1,JDKインストール SE Development Kit 8 - Downloads

2,Android Studioインストール Android Studio

3,日本語化 https://github.com/yuuna/IDEA_resources_jp

[resources_jp.jar]ファイルをAndroid Studioアプリのlibフォルダに突っ込む。再起動。

 

4,Android Studio環境設定

Preference→Editor→各自好みに設定

ちなみに僕は、行番号表示、フォントサイズ、スキーム、オートインポートなどを変更。

 

5,エミュレーターの設定

これをちゃんと設定しないと遅くて使い物になりません。

SDK Managerを起動し、ターゲットとするAPIの[Intel x86 Atom System Image]と[intel x86 Emulator Accelerator (HAXM installer), rev 6.0.1]をチェックして適用。

これだけだと[IntelHAXM]はダウンロードしただけなので[IntelHAXM_6.0.1.dmg]ファイルを探します。

Library/Android/sdk/extras/intel/Hardware_Accelerated_Execution_Manager

にありました。

RAMを1024MBでインストール。

 

AVDManagerを起動し、デバイスを作成します。

機種を選択→ABIがx86APIを選択→Scaleを4dp on device = 1px on scleenに変更→RAMを1024MBに変更。

 

これでエミュレーターの爆速設定完了です。

適当なプロジェクトを作成し、実行してみてください。