Android application development final homework - Part 2

(3) Using imageView, imagebutton and activity to browse pictures on the interface

Create a new android project such as AI03, write the following code in the / AI03/src/com/example/ai03/MainActivity.java file of the project, and pay attention to the package name and the location where the pictures are stored!

The image storage directory is / ai03 / RES / drawable hdpi


package com.example.ai03;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

	//Declare Image object and imagebouton object  
    private ImageView ivwPicture=null;  
    private ImageButton ibtnProv=null;  
    private ImageButton ibtnNext=null;  
    //Declare 5 images  
    private Integer[] iImages = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
    
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        //Get ImageView object ivwPicture  
        ivwPicture=(ImageView)super.findViewById(R.id.picture);  
        //Get two button objects ImageButton  
        ibtnProv=(ImageButton)super.findViewById(R.id.prov);  
        ibtnNext=(ImageButton)super.findViewById(R.id.next);  
        //Register OnClick listener  
        ibtnProv.setOnClickListener(new ProvOnClickListener());  
        ibtnNext.setOnClickListener(new NextOnClickListener());  
        //Register OnlongClick listener  
        ivwPicture.setOnLongClickListener(new PicOnLongClick());  
    }  
    //Click the previous button to display the previous picture  
    private class ProvOnClickListener  implements OnClickListener{  
        private int i=5;  
        public void onClick(View view){           
            if(i > 0){  
                ivwPicture.setImageResource(iImages[--i]);  
            }  
            else if(i == 0){  
                i =5;  
                ivwPicture.setImageResource(iImages[4]);  
            }  
        }  
    }  
    //Click the next button to display the next picture  
    private class NextOnClickListener implements OnClickListener{  
        private int i=0;  
        public void onClick(View view){           
            if(i < 5)  
                ivwPicture.setImageResource(iImages[i++]);  
            else if(i == 5){  
                i = 0;  
                ivwPicture.setImageResource(iImages[0]);  
            }  
        }  
    }  
    //Long press picture to set as desktop wallpaper  
    private class PicOnLongClick implements OnLongClickListener{  
        @Override  
        public boolean onLongClick(View view){  
            try{  
                //Clear current wallpaper  
                MainActivity.this.clearWallpaper();  
                //Current view converted to ImageView object  
                ImageView iv=(ImageView)view;  
                //Enable graph buffering  
                iv.setDrawingCacheEnabled(true);  
                //Create a Bitmap with the current buffer graph  
                Bitmap bmp=Bitmap.createBitmap(iv.getDrawingCache());  
                //Current drawing set to wallpaper  
                MainActivity.this.setWallpaper(bmp);  
                //Clean up drawing buffers  
                iv.setDrawingCacheEnabled(false);  
                Toast.makeText(getApplicationContext(), "Background setting succeeded!",Toast.LENGTH_LONG).show();  
            }  
            catch(Exception e){  
                Toast.makeText(getApplicationContext(), "Background setting failed!",Toast.LENGTH_LONG).show();  
            }  
            return true;  
        }  
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
Write the following code in the project's / ai03 / RES / layout / activity · main.xml file.
<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.example.ai03.MainActivity" >

    <ImageView  
        android:id="@+id/picture"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="0dp"  
        android:src="@drawable/a"  
        tools:ignore="ContentDescription" />

    <ImageButton
        android:id="@+id/prov"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/prov" />

    <ImageButton
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/picture"
        android:src="@drawable/next" />

</RelativeLayout>
Add the following code to the / AI03/AndroidManifest.xml file of the project.

<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ai03"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />
    
    <uses-permission android:name="android.permission.SET_WALLPAPER"/>

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
The operation effect is as follows.



From: http://blog.csdn.net/jianghuiquan/article/details/8348680

(4) Realize the functions and effects as shown in the figure with RadioButton, CheckBox, Activity, etc

Create a new android project such as AI04, write the following code in the project's / AI01/src/com/example/ai04/MainActivity.java file, and pay attention to the package name!

package com.example.ai04;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

	RadioGroup rg;
    TextView show;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//Get the rg and show components on the interface
        rg=(RadioGroup)findViewById(R.id.rg);
        show=(TextView)findViewById(R.id.show);
        //Binding event listeners for OnCheck events of RadioGroup components
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                //Dynamically change the value of tip string according to the radio button checked by the user
                String tip=checkedId==R.id.male?"Your gender is male":"Your gender is a woman";
                  //Modify the text in the show component
                show.setText(tip);
            }
            
        });
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
Write the following code in the project's / ai04 / RES / layout / activity · main.xml file.
<TableLayout 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.example.ai04.MainActivity" >

    <TableRow >
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gender:"
        android:textSize="16dp"/>
    <!-- Define a set of radio buttons -->
    <RadioGroup android:id="@+id/rg"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal">
        <!-- Define two radio buttons -->
        <RadioButton android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/male"
            android:text="male"
            android:checked="true" />
        <RadioButton android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/female"
            android:text="female"/>
    </RadioGroup>
</TableRow>
   <TableRow >
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Favorite colors:"
           android:textSize="16dp"/>
       <!-- Define a vertical linear layout -->
       <LinearLayout
           android:layout_gravity="center_horizontal"
           android:orientation="vertical"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" >
           <!-- Define three check boxes -->
           <CheckBox android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="Red"
               android:checked="true" />
           <CheckBox android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="blue"/>
           <CheckBox android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="green"/>
       </LinearLayout>
   </TableRow>
   <TextView android:id="@+id/show"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />

</TableLayout>
The operation effect is as follows.


From: https://www.cnblogs.com/wolipengbo/p/3343443.html
















Tags: Android xml Java encoding

Posted on Sun, 03 May 2020 13:08:11 -0400 by gwizz