目前分類:【Android】 (13)

瀏覽方式: 標題列表 簡短摘要
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:textColor="#0000FF"
        android:text="@string/photos" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>


每一個項目(item)的選項內容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView android:id="@+id/imageView"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:contentDescription="@string/img_desc"
        android:padding="6dp" />

    <TextView android:id="@+id/textView"
        android:layout_gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:padding="6dp" />

</LinearLayout>


package com.lcpan.listview;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    //A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();//因為現在程式碼很大了,所以寫在方法裡,呼叫方法
    }

    private void findViews() {
        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new MyAdapter(MainActivity.this)); //把MyAdapter指定給ListView元件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {//
                // 選定某個選項(item)時會呼叫此方法
                // view代表被點選的元件,在此為listview_item.xml的LinearLayout元件(也可以說就是整個listview_item.xml)
                TextView textView = (TextView) ((LinearLayout) view).getChildAt(1);
                //指listview_item.xml的LinearLayout元件中的第二個元素TextView android:id="@+id/textView"
                Toast.makeText(getApplicationContext(), textView.getText(),
                        Toast.LENGTH_SHORT).show();//出現彈跳視窗
            }
        });
    }

    private class MyAdapter extends BaseAdapter {//由於繼承BaseAdapter,所以得覆寫四種方法
        //Adapter是一個轉接器,接收資料、丟出資料
        private LayoutInflater layoutInflater;//Inflater:解壓縮、產生器
        private Integer[] images = {//把圖片放入陣列中
                R.drawable.p01, R.drawable.p02, R.drawable.p03, R.drawable.p04,
                R.drawable.p05, R.drawable.p06, R.drawable.p07, R.drawable.p08,
                R.drawable.p09, R.drawable.p10, R.drawable.p11, R.drawable.p12
        };

        public MyAdapter(Context context) {//建構子,建立一個layout物件
            layoutInflater =
                    (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        }

        private class ViewHolder {
            ImageView imageView;
            TextView textView;
        }

        @Override //取得資料數量(12張照片),也就是內容有多少個item
        public int getCount() {
            return images.length;
        }

        @Override //???不用動,position從0開始
        public Object getItem(int position) {
            return position;
        }

        @Override //???不用動
        public long getItemId(int position) {
            return position;
        }

        @Override //系統互依據getCount()的回傳值決定呼叫此方法的次數,第一次呼叫時position=0,第二次=1
        //每回呼叫getView都要配置好每個item的內容,應該類似迴圈
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;//ViewHolder是自己寫的方法
            // convertView為上次呼叫getView()所取得的View,指的是listView元件每個item的layout物件
            if (convertView == null) {// 若convertView已存在,就無需再呼叫inflate()(解壓縮)重新產生它
                convertView = layoutInflater.inflate(R.layout.listview_item, null);//null→預設最外層的layout
        //                inflate(int resource, ViewGroup root)
        //                Inflate a new view hierarchy from the specified xml resource.
                //指定listview_item以產生convertView
                viewHolder = new ViewHolder();
                viewHolder.textView = (TextView) convertView.findViewById(R.id.textView);
                viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
                // 利用setTag()將convertView和viewHolder建立View元件之間的階層關聯
                // convertView為父元件,而viewHolder為子元件
                convertView.setTag(viewHolder);//把viewHolder跟convertView綁在一起
            } else {
                viewHolder = (ViewHolder) convertView.getTag();//取出之前放在裡面的東西
            }
            viewHolder.imageView.setImageResource(images[position]);
            viewHolder.textView.setText("image " + (position + 1));
            return convertView;
        }
    } // end of class MyAdapter

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

乙方 發表在 痞客邦 留言(0) 人氣()

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iii.lcpan.activity">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".NextActivity"></activity>
</application>
</manifest>



<resources>
    <string name="app_name">Activity</string>
    <string name="cel_lbl">攝氏:</string>
    <string name="fah_lbl">華氏:</string>
    <string name="btn_lbl">轉換成華式</string>
    <string name="msg">必須輸入值</string>
    <string name="lastpage">回上頁</string>
</resources>



第一頁:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.iii.lcpan.cf2.MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/cel_lbl" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/celsius" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_lbl"
            android:id="@+id/submit" />
    </LinearLayout>
</RelativeLayout>



第二頁:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.iii.lcpan.activity.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/fah_lbl"
        android:id="@+id/fahrenheit" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/lastpage"
        android:id="@+id/lastpage"
        android:layout_marginTop="91dp"
        android:layout_below="@+id/fahrenheit"
        android:layout_centerHorizontal="true" />
</RelativeLayout>


package com.iii.lcpan.activity;

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

public class MainActivity extends AppCompatActivity {//implements View.OnClickListener {
    private EditText cel;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cel = (EditText) findViewById(R.id.celsius);
        btn = (Button) findViewById(R.id.submit);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    double f = (Double.parseDouble(cel.getText().toString())) * 9 / 5 + 32;
                    Intent intent = new Intent(MainActivity.this, NextActivity.class); //Intent(意圖)使用Bundle物件來傳送資料(本頁,要傳去的那頁)
                    Bundle bundle = new Bundle();
                    bundle.putDouble("f", f);
                    intent.putExtras(bundle);
                    startActivity(intent);
                } catch (Exception e) {
                    Toast.makeText(MainActivity.this, R.string.msg, Toast.LENGTH_LONG).show();

                }


            }
        });
    }

}


package com.iii.lcpan.activity;

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 NextActivity extends AppCompatActivity {//implements View.OnClickListener
    private Button btn;
    private TextView fah;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.next);

        fah = (TextView) findViewById(R.id.fahrenheit);
        Bundle bundle = getIntent().getExtras();
        double f = bundle.getDouble("f");
        fah.setText(getText(R.string.fah_lbl) + (Double.valueOf(f).toString()));

        btn = (Button) findViewById(R.id.lastpage);
//        btn.setOnClickListener(this);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
    }
}

乙方 發表在 痞客邦 留言(0) 人氣()

資料夾呈現方式先改成project,之後建立特定資料夾(注意檔案名稱),並在各資料夾中放他們的string.xml檔

乙方 發表在 痞客邦 留言(0) 人氣()

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.iii.lcpan.activity">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".NextActivity"></activity>
        注意不可以複製 <intent-filter>等等,那會使他產生兩個應用程式
    </application>

</manifest>



<resources>
    <string name="app_name">Activity</string>
    <string name="firstpage">第一頁</string>
    <string name="secondpage">第二頁</string>
    <string name="nextpage">下一頁</string>
    <string name="lastpage">上一頁</string>
</resources>



第一個畫面:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.iii.lcpan.activity.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/firstpage"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/nextpage"
        android:id="@+id/button1"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:clickable="false" />
</RelativeLayout>

第二個畫面:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.iii.lcpan.activity.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/secondpage"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/lastpage"
        android:id="@+id/button2"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:clickable="false" />
</RelativeLayout>


第一頁轉第二頁的程式:

package com.iii.lcpan.activity;

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

public class MainActivity extends AppCompatActivity {//implements View.OnClickListener {<<<<因為用了匿名class,所以就不用implements了
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.button1);
//        btn.setOnClickListener(this);<<<<不用匿名的話就事先連結MainActivity 到button

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(); //Intent(意圖),用以切換頁面,這個方式每用一次就會產生一個新的activity
                intent.setClass(MainActivity.this, NextActivity.class);//setClass(本頁,要去的那一頁.class)
                startActivity(intent);//真正行動了
            }
        });
    }
//    @Override //不用匿名方法的話
//    public void onClick(View v) {
//        Intent intent=new Intent();
//        intent.setClass(this,NextActivity.class);
//        startActivity(intent);
//    }
}



第二頁轉回第一頁的程式:

乙方 發表在 痞客邦 留言(0) 人氣()

res/menu(名稱不可更改) 新建(new)Directory 名稱:menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">
    <item android:id="@+id/about"  //對應到MainActivity中用以取值,用id判斷按了哪個按鈕(呼叫哪個方法)
        android:title="@string/menu_about" //對應到string.xml
        android:icon="@drawable/help_browser"
        android:orderInCategory="1" //代表這個東西在扛棒上的位置,越小越左(上?),同數字的話已寫在上面的優先
        app:showAsAction="always"/> //是否一開式窗及產生這個東西在扛棒上
    <item android:id="@+id/exit"
        android:title="@string/menu_exit"
        android:icon="@drawable/emblem_unreadable"
        android:orderInCategory="2"
        app:showAsAction="always"/>

</menu>



package com.lcpan.i18n;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.about: showAboutDialog(); break;
            case R.id.exit: finish(); break;
//            android.app.Activity是AppCompatActivity爺爺,finish ()定義在爺爺中
//            void finish ()
//            Call this when your activity is done and should be closed.
//            The ActivityResult is propagated back to whoever launched you via onActivityResult().
        }
        return super.onOptionsItemSelected(item);
    }

    private void showAboutDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.about_title);
        builder.setMessage(R.string.about_msg);
        builder.setPositiveButton(R.string.confirm,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {}
                }
        );
        builder.show();
    }
}

乙方 發表在 痞客邦 留言(0) 人氣()

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.iii.lcpan.alertdialog.MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/cel_lbl" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/celsius" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_lbl"
            android:id="@+id/submit" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/fah_lbl"
            android:id="@+id/fahrenheit" />

    </LinearLayout>
</RelativeLayout>



<resources>
    <string name="app_name">AlertDialog</string>
    <string name="cel_lbl">攝氏:</string>
    <string name="fah_lbl">華氏:</string>
    <string name="aaa">測試</string>
    <string name="btn_lbl">轉換成華式</string>
    <string name="title">錯誤</string>
    <string name="msg">必須輸入值</string>
    <string name="confirm">確認</string>
</resources>



package com.iii.lcpan.alertdialog;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity  implements View.OnClickListener{
    private EditText cel;
    private TextView fah;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cel=(EditText) findViewById(R.id.celsius);
        fah=(TextView) findViewById(R.id.fahrenheit);
        btn=(Button) findViewById(R.id.submit);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        try {
            double f = (Double.parseDouble(cel.getText().toString())) * 9 / 5 + 32;
            fah.setText(getText(R.string.fah_lbl) + (Double.valueOf(f).toString()));
        }catch(Exception e){
            showAlertDialog();
//            Toast.makeText(this,R.string.msg,Toast.LENGTH_LONG).show();
        }
    }
    private void showAlertDialog(){
       AlertDialog.Builder builder=new AlertDialog.Builder(this);
        //this是MainActivity物件,此物件是Context的兒子
        //new AlertDialog.Builder(this); 代表這個Alert要跟MainActivity這個activity物件作連接
        //        public Builder(Context context) {
        //            this(context, resolveDialogTheme(context, 0));
        //        }
        builder.setTitle(R.string.title); //設定彈跳出的視窗的標題
        builder.setMessage(R.string.msg); //設定彈跳視窗內文
        //setPositiveButton(string , 按完按鈕之後要做的事情)
        builder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        fah.setText(getText(R.string.aaa));
                        //這個setText只能用設定在string.xml中文字,不能直接打字
                    }
                }
        );
        builder.show();
        //-------------------------------------------------------------
         //另一種寫法(串起來)
//        new AlertDialog.Builder(this)
//        .setTitle(R.string.title)
//        .setMessage(R.string.msg)
//        .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() { 
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//
//                    }
//                }
//        )
//        .show();
    }
}

乙方 發表在 痞客邦 留言(0) 人氣()

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/color"
        android:id="@+id/textView" />

    <Spinner
        android:id="@+id/color"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/output"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="@string/hello"
        android:layout_below="@+id/color"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>




<resources>
    <string name="app_name">Spinner</string>
    <string name="action_settings">Settings</string>
    <string name="color">Color</string>
    <string name="hello">Hello World!!</string>
    <string-array name="colors">
        <item>BLUE</item>
        <item>GREEN</item>
        <item>YELLOW</item>
        <item>RED</item>
        <item>BLACK</item>
    </string-array>
</resources>




package com.lcpan.spinner;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    private Spinner color;
    private TextView output;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        color = (Spinner) findViewById(R.id.color);
        output = (TextView) findViewById(R.id.output);
        color.setOnItemSelectedListener(this);

        String[] colors = getResources().getStringArray(R.array.colors);
        //(R.array.colors)是string.xml中設定的<string-array name="colors"><item></item></Spinner>
        //getResources()是去看R.java檔中的資料,.getStringArray代表尋找string-array的資料
        ArrayAdapter<String> color_adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, colors);
        //ArrayAdapter<String>(MainActivity物件,想要的下拉式選單樣式, 選單內容陣列)
// ArrayAdapter(Context context, int resource, T[] objects) 
   /* ArrayAdapter<CharSequence> color_adapter = ArrayAdapter.createFromResource(this, R.array.colors,
                  android.R.layout.simple_spinner_item);
   */
        color_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
/ Sets the layout resource to create the drop down views. 
        color.setAdapter(color_adapter);         
          Spinner.setAdapter(SpinnerAdapter adapter)
          Sets the SpinnerAdapter used to provide the data which backs this Spinner.
 
    }

    public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
        String itemColor = (String) parent.getSelectedItem();
        if (itemColor.equals("BLUE"))
            output.setTextColor(Color.BLUE);
        else if (itemColor.equals("GREEN"))
            output.setTextColor(Color.GREEN);
        else if (itemColor.equals("YELLOW"))
            output.setTextColor(Color.YELLOW);
        else if (itemColor.equals("RED"))
            output.setTextColor(Color.RED);
        else if (itemColor.equals("BLACK"))
            output.setTextColor(Color.BLACK);
    }

    public void onNothingSelected(AdapterView<?> parent) {
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

乙方 發表在 痞客邦 留言(0) 人氣()

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <ImageButton
        android:id="@+id/imgbtn"
        android:contentDescription="@string/img_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/bg_star_icon" /> //放在drawable資料夾底下的bg_start_icon.png,為一開始的預設圖(?)

</RelativeLayout>



package com.lcpan.imagebutton;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageButton imgbtn;
    private int click=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgbtn = (ImageButton)findViewById(R.id.imgbtn);
        imgbtn.setOnClickListener(this);
    }

    public void onClick(View v) {
        Bitmap img;
      if(click%2==0)
        img = BitmapFactory.decodeResource(getResources(), R.drawable.bg_moon_icon);
          decodeResource(Resources res, int id)

                  Synonym for decodeResource(Resources, int, android.graphics.BitmapFactory.Options) with null Options.

                 getResources()把所有R.java中的設定都抓出來,後面的int是用以抓特定對象的索引值

乙方 發表在 痞客邦 留言(0) 人氣()

<resources>
    <string name="app_name">CheckToggle</string>
    <string name="action_settings">Settings</string>
    <string name="married">已婚</string>
    <string name="single">未婚</string>
    <string name="working">有工作</string>
    <string name="notworking">待業中</string>
    <string name="haskid">有小孩</string>
    <string name="nokid">沒小孩</string>
</resources>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <CheckBox
        android:id="@+id/married"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/married" />

    <ToggleButton
        android:id="@+id/job"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="@string/working"
        android:textOff="@string/notworking" />

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New ToggleButton"
        android:id="@+id/kid"
        android:textOff="@string/nokid"
        android:textOn="@string/haskid" />

    <TextView
        android:id="@+id/output"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""    />

</LinearLayout>



package com.lcpan.checktoggle;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity implements OnClickListener {
    private CheckBox married;
    private ToggleButton job;
    private ToggleButton kid;
    private TextView output;
    private String msg1, msg2, msg3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        married = (CheckBox)findViewById(R.id.married);
        job = (ToggleButton)findViewById(R.id.job);
        kid = (ToggleButton)findViewById(R.id.kid);
        output = (TextView)findViewById(R.id.output);
        msg1 = getString(R.string.single);
        msg2 = ", " + getString(R.string.notworking);
        msg3= ", " + getString(R.string.nokid);
        output.setText(msg1 + msg2+msg3);
        married.setOnClickListener(this);
        job.setOnClickListener(this);
        kid.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch(v.getId()){
            case R.id.married:
                if (((CheckBox)v).isChecked())
                    msg1 = getString(R.string.married);
                else
                    msg1 = getString(R.string.single);
                break;
            case R.id.job:
                if (((ToggleButton)v).isChecked())
                    msg2 = ", " + getString(R.string.working);
                else
                    msg2 = ", " + getString(R.string.notworking);
                break;
            case R.id.kid:
                if (((ToggleButton)v).isChecked())
                    msg3 = ", " + getString(R.string.haskid);
                else
                    msg3 = ", " + getString(R.string.nokid);
                break;
        }
        output.setText(msg1 + msg2+msg3);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

乙方 發表在 痞客邦 留言(0) 人氣()

<resources>
    <string name="app_name">RadioButton</string>
    <string name="action_settings">Settings</string>

    <string name="hello_world">Hello world!</string>
    <string name="yellow">Yellow</string>
    <string name="green">Green</string>
</resources>



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <RadioGroup
        android:id="@+id/group1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="56dp"
        android:layout_marginTop="78dp"
        android:orientation="horizontal" >   垂直分布

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:text="@string/yellow"
            android:checked="true" />    預設值

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:text="@string/green" />
    </RadioGroup>

    <TextView
        android:id="@+id/output"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/group1"
        android:layout_marginLeft="60dp"
        android:layout_marginTop="22dp"
        android:text="@string/hello_world" />

</RelativeLayout>


package com.lcpan.radiobutton;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private TextView output;
    private RadioGroup group1;
    private RadioButton radio1, radio2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        output = (TextView)findViewById(R.id.output);
        group1 = (RadioGroup)findViewById(R.id.group1);
        radio1 = (RadioButton)findViewById(R.id.radio1);
        radio2 = (RadioButton)findViewById(R.id.radio2);
        group1.setOnCheckedChangeListener(this);
    }
    @Override
    public void onCheckedChanged(RadioGroup rg, int checkedid) {
        if (checkedid == radio1.getId()) //checkedid是被選取的ID
            output.setTextColor(Color.YELLOW);
        else if (checkedid == radio2.getId())
            output.setTextColor(Color.GREEN);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       // getMenuInflater().inflate(R.menu.menu_main, menu); 沒有用到
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       // int id = item.getItemId();

       // if (id == R.id.action_settings) {
        //    return true;
      //  }

        return super.onOptionsItemSelected(item);
    }
}

乙方 發表在 痞客邦 留言(0) 人氣()

<resources>
    <string name="app_name">ChangeColor</string>
    <string name="yellow">yellow</string>
    <string name="green">green</string>
    <string name="blue">blue</string>
    <string name="output">Hello World!</string>
</resources>



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.iii.lcpan.changecolor.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/yellow"
        android:id="@+id/yellow"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="126dp"
        android:textAllCaps="false"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/green"
        android:id="@+id/green"
        android:layout_alignTop="@+id/yellow"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/blue"
        android:id="@+id/blue"
        android:layout_alignTop="@+id/green"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/output"
        android:id="@+id/output"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>



package com.iii.lcpan.changecolor;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView output;
    private Button yellow , green , blue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        output=(TextView) findViewById(R.id.output);
        yellow =(Button)findViewById(R.id.yellow);
        green =(Button)findViewById(R.id.green);
        blue =(Button)findViewById(R.id.blue);
        yellow.setOnClickListener(this);
        green.setOnClickListener(this);
        blue.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) { 你按了哪一個鈕,就傳回那個鈕的view物件
        switch(v.getId()){ //取得那個鈕的id,v.getId()取得的是16進元的數字0x1234這樣
            case R.id.yellow : output.setTextColor(Color.YELLOW); break;
            case R.id.green : output.setTextColor(Color.GREEN); break;
            case R.id.blue : output.setTextColor(Color.BLUE); break;
        }
    }
}

乙方 發表在 痞客邦 留言(0) 人氣()

<resources>
    <string name="app_name">CF2</string>
    <string name="cel_lbl">攝氏:</string>
    <string name="fah_lbl">華氏:</string>
    <string name="btn_lbl">轉換成華式</string>
</resources>
 


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.iii.lcpan.cf2.MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/cel_lbl" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/celsius" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_lbl"
            android:id="@+id/submit" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/fah_lbl"
            android:id="@+id/fahrenheit" />

    </LinearLayout>
</RelativeLayout>


package com.iii.lcpan.cf2;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText cel;
    private TextView fah;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  
           setContentView(View view)
           Set the activity content to an explicit view.
        cel=(EditText) findViewById(R.id.celsius);
        fah=(TextView) findViewById(R.id.fahrenheit);
        btn=(Button) findViewById(R.id.submit);
            findViewById(int id)
            Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).
        btn.setOnClickListener(this);
//this就是MainActivity物件
    }

    @Override
    public void onClick(View v) {
        double f=(Double.parseDouble(cel.getText().toString()))*9/5+32;
        fah.setText(getText(R.string.fah_lbl)+(Double.valueOf(f).toString()));
    }
}

乙方 發表在 痞客邦 留言(0) 人氣()

重要檔案:

strings.xml:設定所有要呈現的文字

乙方 發表在 痞客邦 留言(0) 人氣()