为什么不充气的意见作出回应点击听众? [英] Why don't inflated views respond to click listeners?
本文介绍了为什么不充气的意见作出回应点击听众?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想设定一个点击监听器在我的布局的按钮。当我打电话findViewById()时,我从充气布局视图中的点击监听时才会触发直接,而不是:
I'm trying to set a click listener for a button in my layout. The click listener is only triggered when I call findViewById() directly, and not when I take the view from the inflated layout:
public class MyActivity extends Activity implements View.OnClickListener {
private static final String TAG = "MyActivity";
@Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.test );
Button button = (Button)findViewById( R.id.mybutton );
button.setOnClickListener( this );
LayoutInflater inflater = (LayoutInflater)getSystemService( Context.LAYOUT_INFLATER_SERVICE );
ViewGroup rootLayout = (ViewGroup)inflater.inflate( R.layout.test,
(ViewGroup)findViewById( R.id.myroot ), false );
rootLayout.getChildAt( 0 ).setOnClickListener( new View.OnClickListener() {
@Override
public void onClick( View v ) {
Log.d( TAG, "Click from inflated view" );
}
} );
}
@Override
public void onClick( View v ) {
Log.d( TAG, "Click" );
}
}
下面是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myroot" android:orientation="vertical"
android:layout_width="fill_parent" android:background="#ffffff"
android:layout_height="fill_parent">
<Button android:text="Button" android:id="@+id/mybutton"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
这是为什么?我只从第一个方法,而不是从膨胀的观点得到click事件。
Why is this? I only get the click event from the first method and not from the inflated view.
推荐答案
原来我需要调用的setContentView(rootLayout)
。
这篇关于为什么不充气的意见作出回应点击听众?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文