Monday, May 21, 2012 : Article > Android User : Register | Login
บทความ
Webboard
วีดีโอ
บทความจากเพื่อนๆ Guru
03
ต่อจาก Menu และ Preferences บน Android (ตอนที่ 2) เลยนะค่ะ

6.Add Activity จากนั้นเปิด AndroidManifest.xml

เลือก Tab Application (A) แล้วกดปุ่ม Add (B)


เลือก Activity แล้วกด OK


จากนั้น กดปุ่ม Browse..


เลือกตามภาพแล้วกด OK


ใส่ Label เป็น Preferences แล้ว Save (Ctrl+S)


เปิด HelloPreferences.java จากนั้นแก้ไขโค้ดดังนี้

 

package th.co.bighead.exsample.HelloPreferences;

th.co.bighead.exsample.HelloPreferences;

 

import android.app.Activity;

android.app.Activity;

import android.content.Intent;

android.content.Intent;

import android.content.SharedPreferences;

android.content.SharedPreferences;

import android.os.Bundle;

android.os.Bundle;

import android.preference.PreferenceManager;

android.preference.PreferenceManager;

import android.view.Menu;

android.view.Menu;

import android.view.MenuInflater;

android.view.MenuInflater;

import android.view.MenuItem;

android.view.MenuItem;

import android.view.View;

android.view.View;

import android.view.View.OnClickListener;

android.view.View.OnClickListener;

import android.widget.Button;

android.widget.Button;

import android.widget.Toast;

android.widget.Toast;

 

public class HelloPreferences extends Activity {

HelloPreferences Activity {

SharedPreferences preferences;

 

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button button = (Button) findViewById(R.id.Button01);

// Initialize preferences

preferences = PreferenceManager.getDefaultSharedPreferences(this);

 

button.setOnClickListener(new OnClickListener() {

 

@Override

public void onClick(View v) {

String username = preferences.getString("username", "n/a");

String password = preferences.getString("password", "n/a");

Toast.makeText(HelloPreferences.this,

"You maintained user: " + username + " and password: " + password,

Toast.LENGTH_LONG).show();

 

}

});

}

 

@Override

public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater inflater = getMenuInflater();

inflater.inflate(R.menu.menu, menu);

return true;

}

 

// This method is called once the menu is selected

@Override

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

// We have only one menu option

case R.id.preferences:

// Launch Preference activity

Intent i = new Intent(HelloPreferences.this, Preferences.class);

startActivity(i);

// A toast is a view containing a quick little message for the user.

Toast.makeText(HelloPreferences.this,

"Here you can maintain your user credentials.",

Toast.LENGTH_LONG).show();

break;

 

}

return true;

}

}


จากนั้นก็รันโปรแกรมได้เลยค่ะ

ผลการรันโปรแกรม

เมื่อกดปุ่ม false จะมีข้อความ (B) ปรากฏ


เมื่อกดที่ปุ่ม MENU (C) ข้อความ Preferences (D) ก็จะปรากฏ และเมื่อกดที่ข้อความ Preferences


จะมีกล่องข้อความให้ใส่ Username และ Password




หวังว่าตัวอย่างนี้คงให้ประโยชน์กับเพื่อน ๆ ได้นะค่ะ...

Post Rating

Comments

There are currently no comments, be the first to post one.

Post Comment

Only registered users may post comments.
Home | Article | Webboard | Video | Blog | Showcase | News
Copyright 2010 by devguru.mobi