您的位置:首页 > 移动开发 > Android开发

Android小例子:圆角按钮、CheckBox使用、退出确认框、Intent传值、Activity显示软件信息

2015-09-02 20:37 447 查看
一个Android综合小例子,含:

1、圆角按钮

2、CheckBox控件使用

3、软件退出时,弹出退出确认对话框

4、Intent启动另一个Activity,并在Activity之间传值

5、通过PackageInfo获取软件版本信息

一、AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hao.seeya"
android:versionCode="4"
android:versionName="0.1.0" >

<application
android:allowBackup="true"
android:icon="@mipmap/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>

<activity
android:name="com.hao.seeya.CheckBoxActivity" >
</activity>
</application>

</manifest>
注意:

1、android:versionName="0.1.0":设置当前APP软件版本号

2、android:name="com.hao.seeya.CheckBoxActivity":必须在AndroidManifest.xml中注册,否则软件崩溃

二、圆角按钮

首先创建一个shape文件:

main\res\drawable\round_anglexml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
android:shape="rectangle" >
<solid android:color="#000000" />
<corners android:radius="10dip" />
</shape>
然后在定义按钮时,加载该shape文件:

main\res\layout\activity_main.xml

<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">

<Button
android:id="@+id/round_angle_btn"
android:background="@drawable/round_angle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#ffffff"
android:text="圆角按钮"/>

</RelativeLayout>



三、CheckBox控件使用、获取软件版本信息

main\res\layout\check_box.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<CheckBox
android:id="@+id/ios_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="iOS" />

<CheckBox
android:id="@+id/android_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Android" />

<TextView
android:id="@+id/copy_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#cc3366" />
</LinearLayout>
这里的Textview用来显示获取到的版本信息、版权信息。

main\java\com\hao\seeya\CheckBoxActivity.java

public class CheckBoxActivity extends Activity {
public static final String IS_IOS_CHECKED = "iOS";
public static final String IS_ANDROID_CHECKED = "Android";

private TextView copyRight;
private CheckBox iOS;
private CheckBox android;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check_box);

iOS = (CheckBox) findViewById(R.id.ios_cb);
android = (CheckBox) findViewById(R.id.android_cb);
copyRight = (TextView) findViewById(R.id.copy_right);

iOS.setOnCheckedChangeListener(checkListener);
iOS.setChecked(MainActivity.iOSCheckState);
android.setOnCheckedChangeListener(checkListener);
android.setChecked(MainActivity.androidCheckState);

try {
String strCopyRight;
PackageManager pm = this.getPackageManager();
PackageInfo pi = pm.getPackageInfo(this.getPackageName(), 0);
strCopyRight = String.format("Version %s\nCopyRight by Hao", pi.versionName);
System.out.println(strCopyRight);
copyRight.setText(strCopyRight);
} catch (PackageManager.NameNotFoundException e) {
System.out.println("Opps, NameNotFoundException!");
}

}

private OnCheckedChangeListener checkListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Intent intent = new Intent();
switch (buttonView.getId()) {
case R.id.ios_cb:
intent.putExtra(IS_IOS_CHECKED, isChecked);
setResult(RESULT_OK, intent);
finish();
break;
case R.id.android_cb:
intent.putExtra(IS_ANDROID_CHECKED, isChecked);
setResult(RESULT_OK, intent);
finish();
break;
default: break;
}
}
};
}
上述代码段中的try{}catch(){}段就是获取版本、版权信息的实现。



四、软件退出时,弹出退出确认对话框

main\java\com\hao\seeya\MainActivity.java

public class MainActivity extends Activity {
public static boolean iOSCheckState = true;
public static boolean androidCheckState = true;
private Button btn;

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

btn = (Button) findViewById(R.id.round_angle_btn);
btn.setOnClickListener(clickListener);
}

View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.round_angle_btn:
Intent intent = new Intent(MainActivity.this, CheckBoxActivity.class);
startActivityForResult(intent, 1);
break;
default:
break;
}
}
};

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
iOSCheckState = data.getBooleanExtra(CheckBoxActivity.IS_IOS_CHECKED, true);
androidCheckState = data.getBooleanExtra(CheckBoxActivity.IS_ANDROID_CHECKED, true);
}
break;
default: break;
}
}

/**
* 退出软件时弹出确认对话框
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
AlertDialog exit = new AlertDialog.Builder(this).create();
exit.setTitle("Warning!");
exit.setMessage("AiMa, exit?");
exit.setButton("Yes", exitListener);
exit.setButton2("No", exitListener);
exit.show();
}

return false;
}

DialogInterface.OnClickListener exitListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case AlertDialog.BUTTON_POSITIVE:
finish();
break;
case AlertDialog.BUTTON_NEGATIVE:
break;
default: break;
}
}
};
}
上述代码段:

1、获取Button对象,并注册点击事件

2、一旦按钮被点击,启动另一个Activity,即CheckBoxActivity

3、从CheckBoxActivity退出后,获取CheckBoxActivity中CheckBox控件的状态

4、当我们要退出软件时,弹出退出确认对话框:注释后的代码

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: