我已经制作了一个基本的Android布局,其中包含框架,图像和每个文本视图。
我遇到的问题是OnClick事件似乎只适用于前两个。我重新阅读了我的代码,无法理解为什么它不适用于所有的framelayouts ......
我对Android很陌生,所以我可能错过了一些明显的东西。
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="3"
android:rowCount="2"
android:background="@android:color/white"
>
<FrameLayout
android:id="@+id/tempframe"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:clickable="true">
<ImageView
android:id="@+id/sunimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:scaleType="fitXY"
app:srcCompat="@drawable/sun" />
<TextView
android:id="@+id/temptext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/white"
android:clickable="false"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="24sp" />
</FrameLayout>
<FrameLayout
android:id="@+id/himidityframe"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:clickable="true">
<ImageView
android:id="@+id/dropimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
app:srcCompat="@drawable/waterdrop" />
<TextView
android:id="@+id/humiditytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/white"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="24sp" />
</FrameLayout>
<FrameLayout
android:id="@+id/windframe"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:clickable="true">
<ImageView
android:id="@+id/windimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:srcCompat="@drawable/wind" />
<TextView
android:id="@+id/windtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/white"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="24sp" />
</FrameLayout>
<FrameLayout
android:id="@+id/clockframe"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:clickable="true">
<ImageView
android:id="@+id/clockimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:srcCompat="@drawable/stationclockl" />
<TextView
android:id="@+id/clocktext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/white"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="24sp" />
</FrameLayout>
<FrameLayout
android:id="@+id/voltageframe"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:clickable="true">
<ImageView
android:id="@+id/voltageimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:srcCompat="@drawable/battery" />
<TextView
android:id="@+id/voltagetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/white"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="24sp" />
</FrameLayout>
<FrameLayout
android:id="@+id/deviceframe"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:clickable="true">
<ImageView
android:id="@+id/deviceimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:srcCompat="@drawable/weatherstation1cinverted" />
<TextView
android:id="@+id/devicetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/white"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="24sp" />
</FrameLayout>
</GridLayout>`
适用的java /活动代码:
public class Current extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_current);
FrameLayout tempframe = (FrameLayout) findViewById(R.id.tempframe);
FrameLayout humidityframe = (FrameLayout) findViewById(R.id.himidityframe);
FrameLayout windframe = (FrameLayout) findViewById(R.id.windframe);
FrameLayout clockframe = (FrameLayout) findViewById(R.id.clockframe);
FrameLayout voltageframe = (FrameLayout) findViewById(R.id.voltageframe);
FrameLayout deviceframe = (FrameLayout) findViewById(R.id.deviceframe);
final TextView temptext = (TextView) findViewById(R.id.temptext);
final TextView humiditytext = (TextView) findViewById(R.id.humiditytext);
final TextView windtext = (TextView) findViewById(R.id.windtext);
final TextView clocktext = (TextView) findViewById(R.id.clocktext);
final TextView voltagetext = (TextView) findViewById(R.id.voltagetext);
final TextView devicetext = (TextView) findViewById(R.id.devicetext);
tempframe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Current.this, "Temperature", Toast.LENGTH_SHORT).show();
}
});
humidityframe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Current.this, "Humidity", Toast.LENGTH_SHORT).show();
}
});
windframe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Current.this, "Wind Speed", Toast.LENGTH_SHORT).show();
}
});
clockframe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Current.this, "Clock/Time", Toast.LENGTH_SHORT).show();
}
});
voltageframe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Current.this, "Voltage", Toast.LENGTH_SHORT).show();
}
});
deviceframe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Current.this, "Devices and Settings", Toast.LENGTH_SHORT).show();
}
});
感谢您的帮助。
答案 0 :(得分:2)
从ImageView和TextView中删除android:clickable=true
。
ImageView / TextView位于FrameLayout之上,因此如果您将android:clickable=true
设置为这些子级,则会干扰点击侦听器而不会委托给父级(FrameLayout)。
实际上,您可以在此xml中的任何位置删除clickable
属性,setOnClickListener
会自动在视图上将Clickable设置为true。