2016-01-06 28 views
1

我是新來的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:點擊= 「true」 或安卓的onClick = 「動作1」,在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

你的圖像視圖與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(); 

} 

這一空白,將執行單擊每次(TAP)的圖像,並啓動其他活動

回到你的主要活動在SolMecAnalysis.class添加以下代碼:

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

謝謝,它的工作,但現在當我去SolMecAnalysis在模擬器,當我按下後退按鈕它不回去的主要活動,它只是關閉應用程序,任何想法如何解決它? – WiMa

+0

是的,讓我編輯我的答案。 – ivan

+0

那裏它應該現在工作@WiMa – ivan

相關問題