//代码1: package com.example.testsdk2; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bu=(Button)findViewById(R.id.bu_1); bu.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this, scActivity.class); startActivity(intent); Toast.makeText(MainActivity.this, "按钮被按下", Toast.LENGTH_LONG).show(); } }); } }
<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.testsdk2.MainActivity" > <TextView android:id="@+id/tx_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/bu_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击按钮进行测试" /> </RelativeLayout>
package com.example.testsdk2; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class scActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
如果activity.findViewById()方法查找的是对象,那么点击第一个activity上面的“点击按钮进行测试”按钮切换到scActivity之后,再次点击该按钮,会第二次弹出Toast提示信息
而且,如果两个activity布局XML文件不一样,在其中一个对另一个布局文件中定义的View组件id进行引用并修改属性会造成程序崩溃。
代码如下:
//代码2: package com.example.testsdk2; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class scActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.sc_activity); Button but=(Button)findViewById(R.id.bu_2); but.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(scActivity.this,"点击修改按钮", Toast.LENGTH_LONG).show(); Button bu1=(Button)findViewById(R.id.bu_1); Toast.makeText(scActivity.this,"查找id没崩溃", Toast.LENGTH_LONG).show(); // bu1.setTextColor(Color.RED); } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/bu_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击启动修改" /> </LinearLayout>
如果activity.findViewById()查找的是id指向的对象,那么为什么代码1中添加的监听事件在scActivity中失效,如果查找的不是对象,那返回的是什么?
nekomeow3357651
相关分类