作为用户,我是这个论坛的新手,我也是Java和Android的新手。对不起,如果有任何语言或其他错误。
因此,我正在尝试为“#final”制作应用程序。学校项目,具有以下用途:
用户看到一张图片,然后是'并且完成了#39;按钮。当用户点击“下一个”时,有11张图片(用户只能看到一张图片),图片编号为。 2出现,如果他再次点击另一个时间,图片没有。 3出现,依此类推。 当用户点击下两次(所以如果他直到第三张图片),然后点击完成后,他将返回主要活动,而不是文本' Klicke auf start um zu beginnen&#39 ; (点击开始开始)文字'你做到了第三张照片'应该出现。 如果他点击了下一个5次(第6张图片)并点击完成,他也会返回主要活动,但文字是“直到第5张图片”。 等等。
我试过这个,但我失败了。
红线是我自己制作的那些,但不幸的是他们不能工作
[B] Java主要活动[/ B]
package ch.OptiLab.visustest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
TextView textView;
Button btn1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.buttonSTART);
btn1.setOnClickListener(this);
}
@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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivityForResult(new Intent(this,VisusActivity.class), 0);
}
public void onActivityResult() {
if (resultCode == R.drawable.v1)textView.setText("you got 01");
else if (resultCode == R.drawable.v2)textView.setText("you got 02");
else if (resultCode == R.drawable.v3)textView.setText("you got 03");
else if (resultCode == R.drawable.v4)textView.setText("you got 04");
else if (resultCode == R.drawable.v5)textView.setText("you got 05");
else if (resultCode == R.drawable.v6)textView.setText("you got 06");
else if (resultCode == R.drawable.v7)textView.setText("you got 07");
else if (resultCode == R.drawable.v8)textView.setText("you got 08");
else if (resultCode == R.drawable.v9)textView.setText("you got 09");
else if (resultCode == R.drawable.v10)textView.setText("you got 10");
}
]
XML主要活动
<RelativeLayout xmlns:android="(external link)"
xmlns:tools="(external link)"
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"
tools:context="ch.OptiLab.visustest.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/Text1"
android:id="@+id/textView"
android:layout_marginTop="84dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:gravity="center_horizontal"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/Text2"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp" />
<Button
android:id="@+id/buttonSTART"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="@string/button1"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="142dp" />
</RelativeLayout>
Java第二次活动
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class VisusActivity extends Activity {
Button next;
ImageView testanzeige;
Button finish;
int i = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visus);
next = (Button)findViewById(R.id.next);
testanzeige = (ImageView)findViewById(R.id.testanzeige);
finish = (Button)findViewById(R.id.finish);
textView = (TextView)findViewById(R.id.textView);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
i++;
// To Set Your 1 Image Do it Thorough Layout
if (i == 1 ) {
//Your 2 Image
testanzeige.setImageResource(R.drawable.v1);
onStop();
setResult(R.drawable.v1);
}
else if (i == 2) {
//Your 3 Image
testanzeige.setImageResource(R.drawable.v2);
onStop();
setResult(R.drawable.v2);
}
else if (i == 3) {
//Your 4 Image
testanzeige.setImageResource(R.drawable.v3);
onStop();
setResult(R.drawable.v3);
}
else if (i == 4) {
// Your 5 Image
//Imagey.setImageResource(R.drawable.Your Image);
testanzeige.setImageResource(R.drawable.v4);
onStop();
setResult(R.drawable.v4);
}
else if (i == 5 ) {
//Your 6 Image
//Imagey.setImageResource(R.drawable.Your Image);
testanzeige.setImageResource(R.drawable.v5);
onStop();
setResult(R.drawable.v5);
}
else if (i == 6) {
//Your 7 Image
//Imagey.setImageResource(R.drawable.Your Image);
testanzeige.setImageResource(R.drawable.v6);
onStop();
setResult(R.drawable.v6);
}
else if (i == 7 ) {
//Your 8 Image
//Imagey.setImageResource(R.drawable.Your Image);
testanzeige.setImageResource(R.drawable.v7);
onStop();
setResult(R.drawable.v7);
}
else if (i == 8 ) {
//Your 9 Image
//Imagey.setImageResource(R.drawable.Your Image);
testanzeige.setImageResource(R.drawable.v8);
onStop();
setResult(R.drawable.v8);
}
else if (i == 9) {
//Your 10 Image
testanzeige.setImageResource(R.drawable.v9);
onStop();
setResult(R.drawable.v9);
}
else if (i == 10) {
//Image 11 Image
testanzeige.setImageResource(R.drawable.v10);
onStop();
setResult(R.drawable.v10);
//next.setText("Finish");
//Toast.makeText(getApplicationContext(), "Your Final Text", Toast.LENGTH_LONG).show();
}
finish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
});
}
}
XML第二活动
<ImageView
android:id="@+id/testanzeige"
android:layout_width="231dp"
android:layout_height="231dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/v2" />
<Button
android:id="@+id/next"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:text="@string/NEXTPIC" />
<Button
android:id="@+id/finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/next"
android:layout_alignBottom="@+id/next"
android:layout_alignParentLeft="true"
android:text="@string/cantread" />
</RelativeLayout>
最重要的字符串:
<string name="Text1">Klicke auf Start um zu beginnen.</string>
<string name="cantread">finish</string>
非常感谢
答案 0 :(得分:1)
您必须使用startActivityForResult
http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent,int)
您在此功能中传递了两个参数。出于本示例的目的,我们假设您将2
作为结果代码传递。
现在,当您即将完成第二项活动时,在致电finish()
之前,您可以使用setResult(position)
,其中position
是您刚刚设置的图片位置。致电完成后,您的主要活动将触发onActivityResult
。
在该方法中,您必须搜索您传递的requestCode
(2),然后您将获得在setResult
上使用的结果代码。
您也可以使用此方法传递包。
修改强>
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == R.drawable.v1)textView.setText("you got 01");
else if (resultCode == R.drawable.v2)textView.setText("you got 02");
else if (resultCode == R.drawable.v3)textView.setText("you got 03");
else if (resultCode == R.drawable.v4)textView.setText("you got 04");
else if (resultCode == R.drawable.v5)textView.setText("you got 05");
else if (resultCode == R.drawable.v6)textView.setText("you got 06");
else if (resultCode == R.drawable.v7)textView.setText("you got 07");
else if (resultCode == R.drawable.v8)textView.setText("you got 08");
else if (resultCode == R.drawable.v9)textView.setText("you got 09");
else if (resultCode == R.drawable.v10)textView.setText("you got 10");
}
答案 1 :(得分:0)
您需要传递finish
按钮显示的最后一张图片索引:
Intent in = new Intent(this, MainActivity.class);
in.putExtra("index", i);
startActivity(in);
在MainActivity
onCreate
中,检查数据是否已通过:
Bundle data = getIntent().getExtras();
if(data != null)
int lastIndex = data.getInt("index");
在此之后,您可以对lastIndex
执行任何操作,例如在TextView
中显示。