单击下一个或上一个按钮后没有imageview的响应

时间:2015-12-05 16:45:19

标签: android button imageview

   content_main.xml
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    TextView android:text="Hello World!" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="prev"
        android:id="@+id/btnprev"

        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="30dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="next"
        android:id="@+id/btnnext"
        android:layout_alignBottom="@+id/btnprev"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/img03"
        android:layout_toEndOf="@+id/btnnext"
        android:layout_toRightOf="@+id/btnnext" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/testprevbtntext"
        android:layout_below="@+id/btnprev"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="37dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/testnextbtntext"
        android:layout_alignTop="@+id/testprevbtntext"
        android:layout_alignRight="@+id/btnnext"
        android:layout_alignEnd="@+id/btnnext" />
</RelativeLayout>

Photoplayer.java

  package com.fyp.user.my5app;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

/**
 * Created by User on 5/12/2015.
 */
public class photoplayer extends Activity {

    int[] imgId={R.drawable.img01,R.drawable.img02,R.drawable.img03};
    private Button btnPrev,btnNext;
    private ImageView imgPhoto;
    int p=0; // 圖片的索引(第幾張圖片)
    int count=imgId.length; // 共有多少張圖片
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 取得資源類別 R 中的介面元件
        btnPrev=(Button)findViewById(R.id.btnprev);
        btnNext=(Button)findViewById(R.id.btnnext);
        imgPhoto=(ImageView)findViewById(R.id.imageView);

        // 設定  button 元件  Click 事件的  listener
        btnPrev.setOnClickListener(btnPrevListener);
        btnNext.setOnClickListener(btnNextListener);
    }

    // 定義 btnPrev 按鈕的onClick() 方法
    private Button.OnClickListener btnPrevListener=new Button.OnClickListener(){
        public void onClick(View v){
            p--;
            if (p<0)
                p=count-1;
            imgPhoto.setImageResource(imgId[p]);
            setTitle("第" + (p+1) + "/" + count);
        }
    };

    // 定義 btnNext 按鈕的onClick() 方法
    private Button.OnClickListener btnNextListener=new Button.OnClickListener(){
        public void onClick(View v){
            p++;
            if (p==count)
                p=0;
            imgPhoto.setImageResource(imgId[p]);
            setTitle("第" + (p+1) + "/" + count);
        }
    };
}



  <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main"
        android:id="@+id/imageView" />

    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

主要活动.java

package com.fyp.user.my5app;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class  MainActivity extends AppCompatActivity {

    @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();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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);
}

}

  

附件是可以显示照片的photoplayer的代码。那里   在我的申请中是两个按钮,即prev和next。当我按上一个,我   可以查看上一张照片,当我点击下一张时,我可以查看下一张照片,i   有photoplayer.java链接布局activity_main.xml,   但是,点击上一个或下一个后没有任何回复。   附件是我的应用程序的3个文件我有3张照片在drawable fle   名为img01,img02,img03

0 个答案:

没有答案