2: 2016-06-25 (土) 10:07:47 njf[6] [7] [8] | 3: 2016-06-25 (土) 14:26:23 njf[6] [9] [10] Rewound to 2 ages ago. at 2016-06-25 (土) 20:10:51 |
||
---|---|---|---|
Line 72: | Line 72: | ||
&ref(newlibrarynavigator.jpg,mw:480,mh:360); | &ref(newlibrarynavigator.jpg,mw:480,mh:360); | ||
+ | |||
+ | |||
+ | **Alert処理の追加 [#n6d6c2ec] | ||
+ | 先ほど追加したライブラリに処理を書いて行きます。まず、anealert/java以下に「njf.jp.anealert.ANEAlert」クラスを加えます。 | ||
+ | |||
+ | &ref(anemainclass.jpg,mw:480,mh:360);(クリックで拡大) | ||
+ | |||
+ | もし自分のと表示がちょっと違うという場合はプロジェクトタブの上のバーの右側、「<>」という部分をクリックして「Android」を選択してください。 | ||
+ | |||
+ | &ref(navichange.jpg,mw:480,mh:360); | ||
+ | |||
+ | ANE本体となる「njf.jp.anealert.ANEAlert」クラスの中は以下の通りです。 | ||
+ | |||
+ | package njf.jp.anealert; | ||
+ | |||
+ | import android.app.Activity; | ||
+ | import android.app.AlertDialog; | ||
+ | |||
+ | public class ANEAlert { | ||
+ | private Activity activity; | ||
+ | private AlertDialog alertDialog; | ||
+ | public ANEAlert(Activity activity){ | ||
+ | this.activity = activity; | ||
+ | } | ||
+ | public void showAlert(String title,String message){ | ||
+ | if(alertDialog == null){ | ||
+ | alertDialog = new AlertDialog.Builder(activity).setTitle(title).setMessage(message).setPositiveButton("OK", null).show(); | ||
+ | }else{ | ||
+ | alertDialog.setTitle(title); | ||
+ | alertDialog.setMessage(message); | ||
+ | alertDialog.show(); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | 単にshowメソッドでalertダイアログを表示するだけです。 | ||
+ | このクラスを作成したら、appモジュールから呼び出せるようにするため、app内のbuild.gradleの中のdependenciesの所に次の1行を加えます。 | ||
+ | |||
+ | compile project(':anealert') | ||
+ | |||
+ | これを加えたらgradleの同期を行うかどうかの表示がエディタの上に出るので、「Sync Now」をクリックして同期します。 | ||
+ | |||
+ | &ref(syncNow.jpg,mw:480,mh:360); | ||
+ | |||
+ | すると、appの下のjavaクラスからもanealertのクラスが参照可能になり、エディターで補完もきくようになります。 | ||
+ | |||
+ | 次にこれをテスト用に実行するためのMainActivityクラスは以下の通りです。 | ||
+ | |||
+ | package njf.jp.anealertsample; | ||
+ | |||
+ | import android.support.v7.app.AppCompatActivity; | ||
+ | import android.os.Bundle; | ||
+ | import android.view.View; | ||
+ | import android.widget.Button; | ||
+ | |||
+ | import njf.jp.anealert.ANEAlert; | ||
+ | |||
+ | public class MainActivity extends AppCompatActivity { | ||
+ | private ANEAlert aneAlert; | ||
+ | private int alertCounter = 0; | ||
+ | @Override | ||
+ | protected void onCreate(Bundle savedInstanceState) { | ||
+ | super.onCreate(savedInstanceState); | ||
+ | setContentView(R.layout.activity_main); | ||
+ | |||
+ | aneAlert = new ANEAlert(this); | ||
+ | Button button = (Button) findViewById(R.id.showAlertButton); | ||
+ | assert button != null; | ||
+ | button.setOnClickListener(new View.OnClickListener() { | ||
+ | @Override | ||
+ | public void onClick(View v) { | ||
+ | alertCounter++; | ||
+ | aneAlert.showAlert("Android",alertCounter + " times!"); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | ); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | **ANE呼び出し処理の追加 [#a17e7947] | ||
+ | **jarファイルの取り出し [#veeed4bc] |
(This host) = https://njf.jp