Tips

短縮ダイヤルアプリ | AndroidBasic卒業生作成アプリ

  • 登録画面
    • EntryActivity.java
    • package com.example.speeddialing;
      
      import android.app.Activity;
      import android.content.ContentValues;
      import android.content.Intent;
      import android.database.sqlite.SQLiteDatabase;
      import android.os.Bundle;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.Toast;
      
      public class EntryActivity extends Activity implements OnClickListener {
      
      	private MyOpenHelper helper = new MyOpenHelper(this);
      	private SQLiteDatabase db;
      
      	@Override
      	protected void onCreate(Bundle savedInstanceState) {
      		super.onCreate(savedInstanceState);
      		setContentView(R.layout.activity_entry);
      
      		Button button = (Button) findViewById(R.id.button1);
      		button.setOnClickListener(this);
      	}
      
      	@Override
      	public void onClick(View v) {
      		db = helper.getWritableDatabase();
      
      		EditText editName = (EditText) findViewById(R.id.editName);
      		EditText editNumber = (EditText) findViewById(R.id.editNumber);
      
      		String name = editName.getText().toString();
      		String number = editNumber.getText().toString();
      
      		if (editName.equals("")) {
      			Toast.makeText(this, "名前を入力してください。", Toast.LENGTH_SHORT).show();
      		} else if (editNumber.equals("")) {
      			Toast.makeText(this, "番号を入力してください。", Toast.LENGTH_SHORT).show();
      		} else {
      			ContentValues values = new ContentValues();
      			values.put(Table.NAME, name);
      			values.put(Table.NUMBER, number);
      			db.insert(Table.TABLE_NAME, Table.NAME, values);
      
      			editName.getEditableText().clear();
      			editNumber.getEditableText().clear();
      
      			Toast.makeText(this, "登録しました。", Toast.LENGTH_SHORT).show();
      
      			Intent intent = new Intent(EntryActivity.this, MainActivity.class);
      			startActivity(intent);
      		}
      	}
      
      }
      
    • activity_entry.xml
    • <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" >
      
          <TextView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="名前" />
      
          <EditText
              android:id="@+id/editName"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="textPersonName" >
      
              <requestFocus />
          </EditText>
      
          <TextView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="電話番号" />
      
          <EditText
              android:id="@+id/editNumber"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" />
      
          <Button
              android:id="@+id/button1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="登録" />
      
      </LinearLayout>
      
  • 項目の詳細画面
    • RevisionActivity.java
    • package com.example.speeddialing;
      
      import android.app.Activity;
      import android.content.ContentValues;
      import android.content.Intent;
      import android.database.Cursor;
      import android.database.sqlite.SQLiteDatabase;
      import android.net.Uri;
      import android.os.Bundle;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.Toast;
      
      public class RevisionActivity extends Activity implements OnClickListener {
      
      	private MyOpenHelper helper = new MyOpenHelper(this);
      	private SQLiteDatabase db;
      	private Cursor c;
      
      	private EditText editName2;
      	private EditText editNumber2;
      	private Long id;
      
      	@Override
      	protected void onCreate(Bundle savedInstanceState) {
      		super.onCreate(savedInstanceState);
      		setContentView(R.layout.activity_revision);
      
      		db = helper.getReadableDatabase();
      
      		Intent intent = getIntent();
      		id = intent.getLongExtra("id", 1);
      
      		c = db.query(Table.TABLE_NAME,
      				new String[] { Table.NAME, Table.NUMBER }, "_id = ?",
      				new String[] { String.valueOf(id) }, null, null, null);
      
      		c.moveToFirst();
      
      		editName2 = (EditText) findViewById(R.id.editName2);
      		editNumber2 = (EditText) findViewById(R.id.editNumber2);
      
      		editName2.setText(c.getString(c.getColumnIndex(Table.NAME)));
      		editNumber2.setText(c.getString(c.getColumnIndex(Table.NUMBER)));
      
      		Button button2 = (Button) findViewById(R.id.button2);
      		Button button3 = (Button) findViewById(R.id.button3);
      		Button button4 = (Button) findViewById(R.id.button4);
      
      		button2.setOnClickListener(this);
      		button3.setOnClickListener(this);
      		button4.setOnClickListener(this);
      	}
      
      	@Override
      	public void onClick(View v) {
      
      		db = helper.getWritableDatabase();
      
      		switch (v.getId()) {
      		case R.id.button2:
      			ContentValues values = new ContentValues();
      			values.put(Table.NAME, editName2.getText().toString());
      			values.put(Table.NUMBER, editNumber2.getText().toString());
      			db.update(Table.TABLE_NAME, values, "_id = ?",
      					new String[] { String.valueOf(id) });
      
      			Toast.makeText(this, "上書きしました。", Toast.LENGTH_SHORT).show();
      
      			editName2.getEditableText().clear();
      			editNumber2.getEditableText().clear();
      
      			Intent intent = new Intent(RevisionActivity.this,
      					MainActivity.class);
      			startActivity(intent);
      			break;
      
      		case R.id.button3:
      			db.delete(Table.TABLE_NAME, "_id = ?",
      					new String[] { String.valueOf(id) });
      			Toast.makeText(RevisionActivity.this, "削除しました。", Toast.LENGTH_SHORT)
      					.show();
      			Intent intent2 = new Intent(RevisionActivity.this,
      					MainActivity.class);
      			startActivity(intent2);
      			break;
      
      		case R.id.button4:
      			Uri uri = Uri.parse("tel:"
      					+ c.getString(c.getColumnIndex(Table.NUMBER)));
      			Intent intent3 = new Intent(Intent.ACTION_CALL, uri);
      			startActivity(intent3);
      			break;
      
      		default:
      			break;
      		}
      
      	}
      
      }
      
    • activity_revision.xml
    • <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" >
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical" >
      
              <TextView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:text="名前" />
      
              <EditText
                  android:id="@+id/editName2"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:ems="10"
                  android:inputType="textPersonName" >
      
                  <requestFocus />
              </EditText>
      
              <TextView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:text="電話番号" />
      
              <EditText
                  android:id="@+id/editNumber2"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" />
          </LinearLayout>
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal" >
      
              <Button
                  android:id="@+id/button2"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="修正" />
      
              <Button
                  android:id="@+id/button3"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="削除" />
          </LinearLayout>
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical" >
      
              <Button
                  android:id="@+id/button4"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:text="電話" />
          </LinearLayout>
      
      </LinearLayout>
      
  • スプラッシュ画面
    • SplashActivity.java
    • package com.example.speeddialing;
      
      import android.app.Activity;
      import android.content.Intent;
      import android.os.Bundle;
      import android.os.Handler;
      import android.view.Window;
      
      public class SplashActivity extends Activity {
      
      	@Override
      	protected void onCreate(Bundle savedInstanceState) {
      		super.onCreate(savedInstanceState);
      		requestWindowFeature(Window.FEATURE_NO_TITLE);
      		setContentView(R.layout.activity_splash);
      
      		Handler handler = new Handler();
      		handler.postDelayed(new Runnable() {
      
      			@Override
      			public void run() {
      				Intent intent = new Intent(SplashActivity.this,
      						MainActivity.class);
      				startActivity(intent);
      			}
      		}, 5000);
      
      	}
      }
      
    • activity_splash
    • <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" >
      
          <ImageView
              android:id="@+id/imageView1"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:src="@drawable/splash" />
      
      </LinearLayout>
      
  • 「データベース」の生成ファイル
    • MyOpenHelper.java
    • package com.example.speeddialing;
      
      import android.content.Context;
      import android.database.sqlite.SQLiteDatabase;
      import android.database.sqlite.SQLiteOpenHelper;
      
      public class MyOpenHelper extends SQLiteOpenHelper {
      
      	public MyOpenHelper(Context context) {
      		super(context, Table.DB, null, 1);
      	}
      
      	@Override
      	public void onCreate(SQLiteDatabase db) {
      		db.execSQL("create table " + Table.TABLE_NAME + "(" + Table.ID
      				+ " integer primary key, " + Table.NAME + " text not null, "
      				+ Table.NUMBER + " text not null );");
      	}
      
      	@Override
      	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      
      	}
      
      }
      
  • Table.java
  • package com.example.speeddialing;
    
    public class Table {
    
    	public static final String DB = "DB";
    	public static final String TABLE_NAME = "person";
    
    	public static final String ID = "_id";
    	public static final String NAME = "name";
    	public static final String NUMBER = "number";
    
    }
    

    Androidアプリ開発の必須知識!JAVAプログラミングを学べる連載リンク

    はじめてのJAVA 連載

    Recent News

    Recent Tips

    Tag Search