【发布时间】:2015-06-29 16:42:49
【问题描述】:
我得到了fragment app。但是fragment_home.xml有一个相对的布局并且有imageviews。我开发了代码sn-p,但是当我点击imageview时,它没有运行intents.imageviews需要-
public class HomeFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
我写在这里是为了通过 imageviews 运行意图。因为我看到 java 不支持多个扩展。比如扩展片段、活动实现......等等。
public class home extends Activity implements View.OnClickListener {
ImageButton firstModule;
ImageButton secondModule;
ImageButton thirdModule;
ImageButton fourModule;
ImageButton fiveModule;
ImageButton sx;
ImageButton seven;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_home);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//init buttons
firstModule = (ImageButton) findViewById(R.id.imageButton1);
secondModule = (ImageButton) findViewById(R.id.imageButton2);
thirdModule = (ImageButton) findViewById(R.id.imageButton3);
fourModule = (ImageButton) findViewById(R.id.imageButton4);
fiveModule = (ImageButton) findViewById(R.id.imageButton5);
sx = (ImageButton) findViewById(R.id.imageButto6);
seven = (ImageButton) findViewById(R.id.imageButton7);
firstModule.setOnClickListener(this);
secondModule.setOnClickListener(this);
thirdModule.setOnClickListener(this);
fourModule.setOnClickListener(this);
fiveModule.setOnClickListener(this);
sx.setOnClickListener(this);
seven.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButton1:
startActivity(new Intent(home.this, MainActivitysld.class));
break;
case R.id.imageButton2:
startActivity(new Intent(home.this, xxx.class));
break;
case R.id.imageButton3:
startActivity(new Intent(home.this, xxx.class));
break;
case R.id.imageButton4:
startActivity(new Intent(home.this, xxxx.class));
break;
case R.id.imageButton5:
startActivity(new Intent(home.this, xxx.class));
break;
case R.id.imageButto6:
startActivity(new Intent(home.this, xxx.class));
break;
case R.id.imageButton7:
startActivity(new Intent(home.this, xxx.class));
break;
}
}
编辑:我添加了fragment_home.xml
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/konu" />
<ImageButton
android:id="@+id/imageButton7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/nt" />
<ImageButton
android:id="@+id/imageButto6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ders" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/soru" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/cikmis" />
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ozel" />
<ImageButton
android:id="@+id/imageButton5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/strateji" />
</LinearLayout>
</ScrollView>
【问题讨论】:
-
xxx是片段或活动 -
只看片段的好教程
-
xxx.classes 名称 xxx.java
-
java中的类应该以大写开头。记住这一点。这样一来,代码就更清晰了。
标签: android android-intent layout fragment