我为测试和学习目的创造了一个小的。它由一个图像按钮组成,动作是在按下图像按钮时显示一个吐司。
我改变了一切,但是吐司消息没有显示出来。代码中没有单个错误或警告。
最后一天,我在这里发布了代码并没有得到答案。
所以这次我上传了源代码。希望有人能解决这个简单的解决方案。
https://drive.google.com/file/d/0B6Ax58vO7SvZcGRwNFlyYzhqZFk/view?usp=sharing
答案 0 :(得分:0)
只需从内容中删除此代码并将其放在MainActivity.java
中public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButton:
Toast.makeText(getApplicationContext(),"You download is resumed",Toast.LENGTH_LONG).show();
break;
}
}
Rememeber
如果在布局文件(xml)中设置任何onClick事件,则必须在将使用该布局的父活动中创建该方法。
这是完整的代码。
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="switchmode.lteonly.lteonlyswitch.MainActivity"
tools:showIn="@layout/app_bar_main">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:clickable="true"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
android:src="@drawable/myimage"
android:onClick="onClick"/>
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButton:
Toast.makeText(getApplicationContext(),"You download is resumed",Toast.LENGTH_LONG).show();
break;
}
}
}
Contentmain.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
ImageButton imgButton =(ImageButton)findViewById(R.id.imageButton);
// imgButton.setOnClickListener(this);
}