请任何人都可以帮助我,因为我是android的新手我无法获得解决方案 - 如果我使用膨胀的布局,按钮OnClick事件不起作用。这是我的usemerge.xml代码,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2996ff" >
<LinearLayout
android:id="@+id/i1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
</LinearLayout>
</RelativeLayout>
toplayout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/butt1"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="20dp"
android:layout_marginTop="24dp"
android:background="@drawable/mybutton"
android:text="@string/schedule" />
</LinearLayout>
MainActivity.java
private LinearLayout lin1;
private Button bt1;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.usemerge);
final LayoutInflater inflater =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
lin1 = (LinearLayout)findViewById(R.id.i1);
View vi=inflater.inflate(R.layout.toplayout,lin1,false);
lin1.addView(vi);
bt1=(Button)vi.findViewById(R.id.butt1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vo) {
// TODO Auto-generated method stub
Intent ain=new Intent(getBaseContext(), ScheduleActivity.class);
startActivity(ain);
}
});
答案 0 :(得分:0)
在AndroidManifest.xml中定义活动:
<activity android:name=".ScheduleActivity"/>
在将Button添加到LinearLayout之前设置单击侦听器:
public class MainActivity extends FragmentActivity {
private LinearLayout lin1;
private Button bt1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.usemerge);
lin1 = (LinearLayout) findViewById(R.id.i1);
View vi = LayoutInflater.from(this).inflate(R.layout.toplayout, lin1, false);
bt1 = (Button) vi.findViewById(R.id.butt1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vo) {
Intent ain = new Intent(MainActivity.this, ScheduleActivity.class);
startActivity(ain);
}
});
lin1.addView(vi);
}
}
答案 1 :(得分:0)
有两个问题......
首先,你已经注释了按钮的查找。
Button bt1=(Button)vi.findViewById(R.id.butt1);
第二个是你有onclick函数在匿名界面中调用活动的startActivity。你可以做两种解决方案。
首先在MainActivity类中实现onClickListener接口
class MainActivity extends Activity implements Button.onClickListener {
//Your code Goes here...
bt1.setOnClickListener(this);
//Rest of your code
@Override
public void onClick(View view){
Intent ain = new Intent(this, ScheduleActivity.class);
startActivity(ain);
}
}
否则您也可以在onclick中执行以下操作:
Intent ain = new Intent(MainActivity.this, ScheduleActivity.class);
MainActivity.this.startActivity(ain);
答案 2 :(得分:0)
请注意,您在开始时执行setContentView(R.layout.usemerge);
,然后进行充气并将按钮绑定到充气视图,这意味着您无论如何都看不到充气的布局(您看到的按钮)屏幕上的膨胀视图不是 bt1 。
在给视图充气后,您应该setContentView(vi);
。
答案 3 :(得分:0)
在Activity
AndroidManifest.xml
<activity android:name=".ScheduleActivity">
而不是getBaseContext()
使用此行
Intent ain=new Intent(MainActivity.this, ScheduleActivity.class);
像这样实施界面OnClickListener
public class MainActivity extends Activity implements OnClickListener {
private LinearLayout lin1;
private Button bt1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LayoutInflater inflater =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
lin1 = (LinearLayout)findViewById(R.id.i1);
View vi=inflater.inflate(R.layout.activity_sub,lin1,false);
lin1.addView(vi);
bt1=(Button)vi.findViewById(R.id.butt1);
bt1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent ain=new Intent(MainActivity.this, SubActivity.class);
startActivity(ain);
}
}
答案 4 :(得分:0)
在膨胀之后添加setContentView(vi);
,然后移除setContentView(R.layout.usemerge);
答案 5 :(得分:-1)
您的MainActivity.java应如下所示:
LinearLayout lin1;
Button bt1;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.usemerge);
LayoutInflater inflater = getLayoutInflater().inflate(R.layout.toplayout, null, true);
bt1 = inflater.findViewById(R.id.butt1);
lin1 = (LinearLayout)findViewById(R.id.i1);
View vi=inflater.inflate(R.layout.toplayout,lin1,false);
lin1.addView(vi);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vo) {
// TODO Auto-generated method stub
Intent ain=new Intent(getBaseContext(), ScheduleActivity.class);
startActivity(ain);
}
});