如何编写图像视图以查看其他活动

时间:2016-01-06 20:35:41

标签: android-studio

我是android studio的新手,我想让我的主要活动中的图像显示点击时的另一个活动,我尝试了很多东西,但我无法让它工作。 (我只添加了1个imageview,但我还会添加4个)。谢谢你的帮助。这是MainActivity.java

package com.wima.civilengineeringcalculator;

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

这里是content_main.XML(哪一个更好用android:clickable =“true”或android:onClick =“action1”,在imageview中)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.wima.civilengineeringcalculator.MainActivity"
tools:showIn="@layout/activity_main"
>



<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="end">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/sma"
            android:id="@+id/textView"
            android:singleLine="false" />

        <ImageView
            android:onClick="onClickTest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView" android:src="@drawable/spr" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="AB"
            android:id="@+id/textView3" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"></TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"></TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"></TableRow>

</TableLayout>

目标活动是SolMecAnalysis

package com.wima.civilengineeringcalculator;

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.content.Intent;

public class SolMecAnalysis extends AppCompatActivity {

Intent intent = getIntent();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sol_mec_analysis);
    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();
        }
    });
}

}

1 个答案:

答案 0 :(得分:1)

你的图片视图正在做什么xml正在尝试执行名为onClickTest的函数,当你的主活动启动它时,你的主活动中不存在这个函数不使用“content_main.xml”

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

表示您在该活动中观看的xml是activity_main.xml, 你也应该在你的主要活动中添加一个像这样的空白:

 public void onClickTest(View view) {
     Intent i = new Intent(this, SolMecAnalysis.class);
     startActivity(i); 
     finish();

 }

每次点击(点击)图像并开始其他活动时都会执行该空白

要返回主要活动,请在SolMecAnalysis.class中添加此代码:

    @Override 
    public void onBackPressed() {
    Intent intent = new Intent(SolMecAnalysis.this, MainActivity.class);
    startActivity(intent);
    finish(); 
    }