当使用膨胀布局按钮单击事件不工作 [英] Button click event is not working when inflated layout is used
问题描述
请谁能帮助我,因为我是新到Android我不能够得到解决 - 如果我使用膨胀布局按钮OnClick事件是行不通的。这里是我的usemerge.xml code,
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=#2996ff>
<的LinearLayout
机器人:ID =@ + ID / I1
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向
>
< / LinearLayout中>
< / RelativeLayout的>
toplayout.xml
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / linear1
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向
> <按钮
机器人:ID =@ + ID / butt1
机器人:layout_width =130dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =24dp
机器人:layout_marginStart =20dp
机器人:layout_marginTop =24dp
机器人:背景=@绘制/ myButton的
机器人:文字=@字符串/时间表/>
< / LinearLayout中>
MainActivity.java
私人的LinearLayout林1;
私人按钮BT1;
保护无效的onCreate(捆绑savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.usemerge); 最后LayoutInflater气筒=
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
林1 =(的LinearLayout)findViewById(R.id.i1);
查看VI = inflater.inflate(R.layout.toplayout,林1,假); lin1.addView(六);
BT1 =(按钮)vi.findViewById(R.id.butt1); bt1.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(查看VO){
// TODO自动生成方法存根 意图AIN =新意图(getBaseContext(),ScheduleActivity.class);
startActivity(AIN);
}
});
请注意,您的setContentView(R.layout.usemerge);
开头,之后你做你的充气和按钮绑定到膨胀认为,这意味着你没有看到膨胀的布局反正(,美在屏幕上看到的按钮是不是 BT1从膨胀的观点)。
您应该做的的setContentView(VI)。
Please can anyone help me, since i am new to android i am not able to get solution for - the button OnClick event is not working if i use the inflated layout. Here is my usemerge.xml code,
<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);
}
});
Notice that you do setContentView(R.layout.usemerge);
at the beginning, and after that you do your inflate and bind the button to the inflated view, this mean that you don't see the inflated layout anyway (the button that u see on the screen isn't bt1 from inflated view).
You should do setContentView(vi);
after inflating the view.
这篇关于当使用膨胀布局按钮单击事件不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!