<LinearLayout
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/btn_image" />
<TextView
android:layout_width="150dp"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:text="something" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
btn1 = (LinearLayout) findViewById(R.id.btn1);
btn1.setOnClickListener(this);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
我无法点击按钮。总是被称为点击LinearLayout。
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
// set something parametr
break;
case R.id.btn1:
// go to activity
break;
}
}
答案 0 :(得分:1)
似乎对我有用,
你的xml文件,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="150dp"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:text="something" />
<Button
android:id="@+id/btn"
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
你的Activity类,
public class MainActivity extends Activity implements OnClickListener{
LinearLayout btn1 =null;
Button btn =null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (LinearLayout) findViewById(R.id.btn1);
btn1.setOnClickListener(this);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onClick(View v) {
if(v==btn)
{
Toast.makeText(getApplicationContext(), "btn clicked", Toast.LENGTH_SHORT).show();
}
if(v==btn1)
{
Toast.makeText(getApplicationContext(), "btn1 clicked", Toast.LENGTH_SHORT).show();
}
}
}
答案 1 :(得分:0)
您仍然可以区分onclick方法:
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn1)
{
// Linear was clicked
}
else if (v.getId() == R.id.btn){
// Button was clicked
}
}
但你仍然应该从线性
中删除按钮答案 2 :(得分:0)
在标签中输入clickable true。
文件 android:clickable =“true”
<LinearLayout
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical" android:onClick="" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/actionbar_background" />
<TextView
android:layout_width="150dp"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:text="something" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true" />
</LinearLayout>
...................................
btn1 = (LinearLayout) findViewById(R.id.btn1);
btn1.setOnClickListener(this);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
...................................
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
// set something parametr
break;
case R.id.btn1:
// go to activity
break;
}
}