Powered By Blogger

2012年5月12日 星期六

Android開發手札-變換字體大小(兩個Activity之間的連結)

這次來分享如何讓兩個Activity互相連結,就如同某些專案有變換字體大小的偏好設定值,此為最簡單的方法。


----------------------------------------Main.java--------------------------------------------

public class Main extends Activity {
    //ContextMenuActivity主程式
        final static int 變換字體顏色及大小 = 1000;
        /** Called when the activity is first created. */
        //登記上下文菜單
        @Override
        public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             requestWindowFeature(Window.FEATURE_NO_TITLE); 
             getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
             
             setContentView(R.layout.main);
             LinearLayout view = (LinearLayout)findViewById(R.id.view1);
             registerForContextMenu(view);
             Intent intent=getIntent();
            
             int position = intent.getIntExtra("color",2);
             int position2 = intent.getIntExtra("size",2);
             int position3 = intent.getIntExtra("null",1);
           
            
   
             TextView myText = (TextView)findViewById(R.id.myText);
             AssetManager assetManager = getAssets();
             InputStream inputStream = null;
         
             String MyStream;
             try {
              // 指定/assets/xxx.txt
         inputStream = assetManager.open("myinfo.txt");
       
         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
         byte[] bytes = new byte[4096];
       
         int len;
         while ((len = inputStream.read(bytes)) > 0){
          byteArrayOutputStream.write(bytes, 0, len);
         }
       
         MyStream = new String(byteArrayOutputStream.toByteArray(), "BIG5");
       
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         MyStream = e.toString();
        }
       
       
      
        myText.setText(MyStream);
        switch(position) {
        case 0:
            // 切換成紅色
           myText.setTextColor(Color.RED);
            break;
        case 1:
            // 切換成黃色
            myText.setTextColor(Color.YELLOW);
          
            break;
        case 2:
            // 切換成藍色
            myText.setTextColor(Color.BLUE);
            break;
        case 3:
            // 切換成綠色
            myText.setTextColor(Color.GREEN);
            break;
        case 4:
            // 切換成白色
            myText.setTextColor(Color.WHITE);
            break;
        case 5:
            // 切換成白色
            myText.setTextColor(Color.CYAN);
            break;
        case 6:
            // 切換成亮灰
            myText.setTextColor(Color.LTGRAY);
            break;
        case 7:
            // 切換成洋紅
            myText.setTextColor(Color.MAGENTA);
            break;
        case 8:
            // 切換成暗灰
            myText.setTextColor(Color.DKGRAY);
            break;   
        case 9:
            // 切換成白色
            myText.setTextColor(Color.BLACK);
            break;   
    } 
       
       
        switch(position2) {
        case 0:
        
            myText.setTextSize(12);
            break;
        case 1:
           
            myText.setTextSize(14);
            break;
        case 2:
           
            myText.setTextSize(16);
            break;
        case 3:
           
            myText.setTextSize(18);
            break;
        case 4:
           
            myText.setTextSize(20);
            break;
        case 5:
           
            myText.setTextSize(22);
            break;
        case 6:
           
            myText.setTextSize(24);
            break;
        case 7:
           
            myText.setTextSize(26);
            break;
        case 8:
           
            myText.setTextSize(28);
            break;   
        case 9:
           
            myText.setTextSize(30);
            break;   
    
    } 
        switch(position3){    //回復原來預設值
        case 0:
        myText.setTextSize(16);
        myText.setTextColor(Color.BLUE);
        break;
        }
   
}
       
    //建立上下文菜單
        @Override
        public void onCreateContextMenu(ContextMenu menu, View v,
                ContextMenuInfo menuInfo) {
            super.onCreateContextMenu(menu, v, menuInfo);
            menu.add(0, 變換字體顏色及大小,0, "變換字體顏色及大小");
   
        }
        //處理上下文菜單
        @Override
        public boolean onContextItemSelected(MenuItem item) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
   
           switch (item.getItemId()) {
            case 變換字體顏色及大小:
                Intent intent = new Intent();
                intent.setClass(Main.this, Main2.class);
                startActivity(intent);
                finish();
                 break;
              }
           
             builder.create();
             return true;
        }
       
}



----------------------------------------Main.xml--------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/view1"
    >
    <TextView android:id="@+id/myText" android:text="TextView" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
</LinearLayout>






----------------------------------------Main2.java--------------------------------------------


public class Main2 extends Activity {

    int position;
    int position2;
    int position3;
 
    static final String[] mDaysList=new String[] {
        "紅色", "黃色", "藍色", "綠色",
        "白色", "天空色", "亮灰色", "洋紅色", "暗灰色", "黑色"
    };   
        static final String[] mDaysList2=new String[] {
            "12", "14", "16", "18",
            "20", "22", "24", "26", "28", "30"
        };
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         requestWindowFeature(Window.FEATURE_NO_TITLE); 
         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
         
         setContentView(R.layout.main2);
        
         TextView textView = (TextView) findViewById(R.id.textView1);
         textView.setTextColor(Color.RED);
         textView.setTextSize(25);
       
         TextView textView2 = (TextView) findViewById(R.id.textView2);
         textView2.setTextColor(Color.RED);
         textView2.setTextSize(25);
        
         Spinner mSpinner1;
         Spinner mSpinner2;
        
        
        
           mSpinner1 = (Spinner) findViewById(R.id.spinner1);
          
     
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, mDaysList);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            mSpinner1.setAdapter(adapter);
            mSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
                  
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int pos, long id) {
                  
            position = pos;
               
                }
               
                public void onNothingSelected(AdapterView<?> parentView) {
              
                }

            });
       
           
            mSpinner2  = (Spinner) findViewById(R.id.spinner2);
            
            ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, mDaysList2);
            adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            mSpinner2.setAdapter(adapter2);
            mSpinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
                  
                public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int pos2, long id) {
                  
               position2 = pos2;
               
                     
                }
               
                public void onNothingSelected(AdapterView<?> parentView) {
              
                }

            });
          
         Button b1 = (Button) findViewById(R.id.button1);
            b1.setOnClickListener(new Button.OnClickListener()
            {
              public void onClick(View v)
              {
                  Intent intent = new Intent();
                     intent.setClass(Main2.this, Main.class);
                   
                     intent.putExtra("color",position);    
                     intent.putExtra("size",position2);    
                     
                    startActivity(intent);
                    finish();
               
              }
            });
           
            //回復預設值
            Button b2 = (Button) findViewById(R.id.button2);
            b2.setOnClickListener(new Button.OnClickListener()
            {
              public void onClick(View v)
              {
                  Intent intent = new Intent();
                     intent.setClass(Main2.this, Main.class);
                     intent.putExtra("null",position3);    
                     startActivity(intent);
                      finish();
              }
            });
         } 
    }

----------------------------------------Main2.xml--------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" 
android:layout_width="match_parent" 
android:orientation="vertical" 
android:layout_height="match_parent">
        <TextView android:text="字型顏色" 
android:id="@+id/textView1"
 android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</TextView>
        <Spinner android:id="@+id/spinner1" 
android:layout_height="wrap_content" 
android:layout_width="match_parent">
</Spinner>
        <TextView android:text="字型大小" 
android:id="@+id/textView2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</TextView>
        <Spinner android:id="@+id/spinner2" 
android:layout_height="wrap_content" 
android:layout_width="match_parent">
</Spinner>
        <LinearLayout android:layout_width="match_parent" 
android:id="@+id/linearLayout2" 
android:layout_height="match_parent" 
android:orientation="horizontal">
            <LinearLayout android:layout_width="wrap_content" android:id="@+id/linearLayout3" 
android:layout_height="match_parent">
                <Button android:text="確定" 
android:id="@+id/button1" 
android:layout_height="wrap_content"
 android:layout_width="wrap_content"></Button>
            </LinearLayout>
            <Button android:text="回復預設值" 
android:id="@+id/button2" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content">
</Button>
        </LinearLayout>
    </LinearLayout>
 

----------------------------------------執行畫面--------------------------------------------



                                              執行畫面會叫出mytext內的文字檔



                                                  長按畫面跳出功能選項


                             此時來到另外一個修改自型顏色及自型大小的Activity

                                               選擇欲修改的樣式接著按確定
          
                             


沒有留言:

張貼留言