Android中控件的初步认识(二)

来源:互联网 发布:大数据面临的问题评价 编辑:程序博客网 时间:2024/05/16 10:45
package com.itarchy.demo;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class HomeActivity extends Activity {private Button btn;private Button btnBack;private int RequestCode = 200;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 1、通过setContentView加装布局setContentView(R.layout.home_main);/** * 一、直接启动Activity */// 通过findViewById找到控件,需要强制转换?????btn = (Button) this.findViewById(R.id.btn);// 3、添加监视器btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// 点击的时跳转到NewActivity// 指定启动源、启动目标// 为什么不能用this?????Intent intent = new Intent(HomeActivity.this, NewActivity.class);startActivity(intent);}});/** * 二、待返回值启动Activity */btnBack = (Button) this.findViewById(R.id.btn_back);btnBack.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(HomeActivity.this,ForResultActivity.class);startActivityForResult(intent, RequestCode);}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);// 100 ???if (requestCode == RequestCode && resultCode == 100) {String resultValue = data.getStringExtra("ForBack");int resultInt = data.getIntExtra("ForBack_int", 1000);Log.i("Tag", "---TAG-HomeActivity-onActivityResult--resultValue="+ resultValue);Log.i("Tag", "---TAG-HomeActivity-onActivityResult--resultInt="+ resultInt);}}}


<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=".HomeActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:text="HomeActivity" />    <Button        android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="直接启动Activity" />    <Button        android:id="@+id/btn_back"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/btn"        android:layout_centerHorizontal="true"        android:text="带返回值启动Activity" /></RelativeLayout>



package com.itarchy.demo;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class ForResultActivity extends Activity implements OnClickListener {// 声明控件private TextView tvResult;private Button btnBack;private String result;private int ResultCode=100;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.forresult_main);initView();setListener();}// 初始化private void initView() {tvResult = (TextView) this.findViewById(R.id.tv_result);btnBack = (Button) this.findViewById(R.id.btn_sure);}private void setListener() {// this?????btnBack.setOnClickListener(this);}// 点击的时候执行onClick方法@Overridepublic void onClick(View v) {result = tvResult.getText().toString();Log.i("Tag", "---TAG-ForResultActivity-->" + result);// 创建Intent对象Intent data = new Intent();// 封装返回值data.putExtra("ForBack", result);data.putExtra("ForBack_int", 500);setResult(ResultCode, data);this.finish();}}

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#ffbbbbbb" >    <TextView        android:id="@+id/tv_result"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:text="ForResultActivity" />    <Button        android:id="@+id/btn_sure"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:text="确定" /></RelativeLayout>

package com.itarchy.demo;import android.app.Activity;import android.os.Bundle;public class NewActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.new_main);}}


<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"         android:layout_centerHorizontal="true"        android:text="NewActivity"/></RelativeLayout>

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.itarchy.demo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.itarchy.demo.HomeActivity"            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.itarchy.demo.NewActivity"/>         <activity android:name="com.itarchy.demo.ForResultActivity"/>    </application></manifest>



0 0
原创粉丝点击