我正在尝试在主屏幕上创建LinearLayout
以充当菜单。我不想要Buttons
或者不得不创建图片,我喜欢TextViews
和Layouts
的外观。我的问题是我不能让onClick()
通过第一个布局,它在过去的几天里让我疯了。有什么小的我想念或我做错了吗?
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout computer = (LinearLayout) findViewById(R.id.main_btn_computer);
LinearLayout networking = (LinearLayout) findViewById(R.id.main_btn_networking);
computer.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
//Call your method here
startActivity (new Intent(getApplicationContext(), ComputerMain.class));
}
});
networking.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
//Call your method here
startActivity (new Intent(getApplicationContext(), NetworkingMain.class));
}
});
}
}
}
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="6dip" >
<LinearLayout
android:id="@+id/main_btn_computer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:background="@drawable/border"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:weightSum="5" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Operating Systems"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/light_blue" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Windows, Linux, Dos, and Mac"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/grey_font" />
</LinearLayout>
<LinearLayout
android:id="@+id/main_btn_networking"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/border"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:weightSum="10" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Networking"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/light_blue" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cisco, HP Procurve"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/grey_font" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
为什么使用onClickFeature(View v)
onClick(View v)
。
正在设置onClickListener()
?
答案 1 :(得分:0)
致电LinearLayout
LinearLayout computer = (LinearLayout) findViewById(R.id.main_btn_computer);
LinearLayout networking = (LinearLayout) findViewById(R.id.main_btn_networking);
设置onClick侦听器
computer.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
//Call your method here
startActivity (new Intent(getApplicationContext(), ComputerMain.class));
}
});
networking.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
//Call your method here
startActivity (new Intent(getApplicationContext(), NetworkingMain.class));
}
});
删除android:onClick=""
属性