Intent II 
Step 1 : 
Step 2 :
Step 3 :
Code :
strings.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Demo Intents-II </string>
    <string name="second_activity_name">Activity : COLORPICKER</string>
    <string name="textlable1">Your most favourite color is</string>
    <string name="textlable2">Choose your favourite color</string>
    <string name="red">RED</string>
    <string name="green">GREEN</string>
    <string name="blue">BLUE</string>
    <string name="buttonText">Pick your favourite color</string>
    <string name="radioRed">I like red</string>
    <string name="radioGreen">I enjoy green</string>
    <string name="radioBlue">I love blue</string>
</resources>
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:gravity="center"
    android:padding="8dp" >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center"
        android:textColor="#ffffff"
        android:text="@string/textlable1" />
    <TextView
        android:id="@+id/lableRed"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="72sp"
        android:textStyle="bold"
        android:textColor="#666666"
        android:text="@string/red" />
        
    <TextView
        android:id="@+id/lableGreen"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="72sp"
        android:textStyle="bold"
        android:textColor="#666666"
        android:text="@string/green" />
        
    <TextView
        android:id="@+id/lableBlue"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="72sp"
        android:textStyle="bold"
        android:textColor="#666666"
        android:text="@string/blue" />
        
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/buttonText"
        android:id="@+id/pickbutton"
        android:onClick="buttonClickHandler" />
</LinearLayout>
picker.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:gravity="center"
  android:padding="8dp" >
  
  <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:textColor="#ffffff"
      android:text="@string/textlable2" />
      
      <RadioGroup
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/radiogroup" >
      
          <RadioButton
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="@string/radioRed"
              android:textColor="#ff0000"
              android:id="@+id/radioRed"
              android:layout_weight="1"
              android:onClick="radioClickHandler" />
          
          <RadioButton
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="@string/radioGreen"
              android:textColor="#00ff00"
              android:id="@+id/radioGreen"
              android:layout_weight="1"
              android:onClick="radioClickHandler" />
          
          <RadioButton
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="@string/radioBlue"
              android:textColor="#0000ff"
              android:id="@+id/radioBlue"
              android:layout_weight="1"
              android:onClick="radioClickHandler" />
          
      </RadioGroup>             
</LinearLayout>
Episode.java
package com.example.Intents;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class Episode11 extends Activity {
    
    static final private int CHOOSE_COLOR = 0 ;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
   private void resetColor(){
       
       TextView tv ;
       
       tv = (TextView) findViewById(R.id.lableRed);
       tv.setTextColor(Color.parseColor("#666666"));
       tv = (TextView) findViewById(R.id.lableGreen);
       tv.setTextColor(Color.parseColor("#666666"));
       tv = (TextView) findViewById(R.id.lableBlue);
       tv.setTextColor(Color.parseColor("#666666"));
   }
   
   public void buttonClickHandler(View target){
    
       Intent question = new Intent(this, com.example.Intents.ColorPicker.class);
       startActivityForResult(question, CHOOSE_COLOR);
   }
   
   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       switch(requestCode)
       {
       case CHOOSE_COLOR :
           if(resultCode==RESULT_OK)
           {
               resetColor();
               String color = data.getExtras().getString("color");
               int lable = data.getExtras().getInt("lable");
               
               TextView tv = (TextView) findViewById(lable);
               tv.setTextColor(Color.parseColor(color));
           }
           break ;
       default :
             break ;
       }
   }
}
ColorPicker.java
package com.example.Intents;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class ColorPicker extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.picker);
    }    
    public void radioClickHandler (View v)
    {
        Intent answer = new Intent();
        
        switch(v.getId())
        {
            case R.id.radioRed :
                answer.putExtra("color", "#ff0000");
                answer.putExtra("lable", R.id.lableRed);
                break ;
            case R.id.radioGreen :
                answer.putExtra("color", "#00ff00");
                answer.putExtra("lable", R.id.lableGreen);
                break ;
            case R.id.radioBlue :
                answer.putExtra("color", "#0000ff");
                answer.putExtra("lable", R.id.lableBlue);
                break ;
        }
        
        setResult(RESULT_OK,answer);
        finish();
    }
}
 
 
 
  
 
Không có nhận xét nào:
Đăng nhận xét