每个人都有单选按钮,我希望所选值显示在我的第二个活动中。当我把额外和意图,它没有显示。 我想我做错了什么或遗失了什么。
MainActivity.java
package com.example.shiva_000.myproject;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import java.io.File;
import java.net.URI;
public class MainActivity extends Activity {
public static final String Tag_Price ="tag";
public static final String TAG_Radio ="Button";
private String SelectedType ="";
String path = "sdcard/camera_app/cam_image.jpg";
EditText et;
Button button;
ImageView imageView;
// Radio button
Button button2;
RadioGroup radio_g;
RadioButton rb1, rb2;
static final int cam_request = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button =(Button) findViewById(R.id.button1);
imageView =(ImageView) findViewById(R.id.image_view);
button2 = (Button) findViewById(R.id.submit);
et =(EditText) findViewById(R.id.editText);
radio_g=(RadioGroup) findViewById(R.id.rg1);
final RadioButton rb1 = (RadioButton) findViewById(R.id.newoption);
final RadioButton rb2 = (RadioButton) findViewById(R.id.usedoption);
// on click for submit
button2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String s = et.getText().toString();
Intent i = new Intent(MainActivity.this,secondScreen.class);
i.putExtra("Condition",SelectedType);
i.putExtra("Price",s);
i.putExtra("image",path);
startActivity(i);
}
});
// on click for capture image
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent camera_Intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
camera_Intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(camera_Intent, cam_request);
}
});
//launchSecondActivity(Double.parseDouble(et.getText().toString()),null);
}
//private void launchSecondActivity(double price ,Image image){
// }
public void Condition(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.newoption:
if (checked)
//SelectedType = rb1.getText().toString();
break;
case R.id.usedoption:
if (checked)
// SelectedType = rb2.getText().toString();
break;
}
}
private File getFile()
{
File folder = new File("sdcard/camera_app");
if (!folder.exists())
{
folder.mkdir();
}
File image_file = new File(folder,"cam_image.jpg");
return image_file;
// return null;
}
// public void
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// String path = "sdcard/camera_app/cam_image.jpg";
imageView.setImageDrawable(Drawable.createFromPath(path));
}
//private void launchSecondActivity(condion , ){
//}
}
secondScreen.java
package com.example.shiva_000.myproject;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
// import android.Graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import java.io.File;
/**
* Created by shiva_000 on 11/18/2015.
*/
public class secondScreen extends Activity {
ImageView imageView;
TextView textView1;
TextView textView2;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
imageView =(ImageView) findViewById(R.id.image_view);
textView1 =(TextView) findViewById(R.id.textView4);
textView2 =(TextView) findViewById(R.id.textView5);
button = (Button)findViewById(R.id.button2);
Intent ii = getIntent();
Bundle b = ii.getExtras();
if (b!=null) {
String passed_condition = (String) b.get("Condition");
String passed_price = (String) b.get("Price");
//String passed_image = getIntent().getStringExtra("imagePath");
textView1.setText("Condition : " + passed_condition);
textView2.setText("The price is : " + passed_price + "$");
//Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
String image = ii.getStringExtra("image");
File imgFile = new File(image);
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap bi =Bitmap.createBitmap(myBitmap, 0, 0, myBitmap.getWidth(), myBitmap.getHeight(), matrix, true);
ImageView myImage = (ImageView) findViewById(R.id.image_view);
myImage.setImageBitmap(bi);
}
//imageView.setImageDrawable();
}
//button.setOnClickListener();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
//String path = getIntent().getStringExtra("")
}
// private void initializesecondactivity ()
//{
// double price = getIntent().getExtras().getDouble(MainActivity.Tag_Price);
// }
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 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" tools:context=".MainActivity"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:layout_width="310dp"
android:layout_height="wrap_content"
android:text="Welcome to SellJunky"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:layout_weight="0.03"
android:textColor="#a91b1b"
android:textIsSelectable="false"
android:textSize="30dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture Image"
android:id="@+id/button1"
android:layout_weight="0.03"
android:layout_gravity="center_horizontal" />
<ImageView
android:layout_width="350dp"
android:layout_height="200dp"
android:id="@+id/image_view"
android:layout_gravity="center_horizontal"/>
<TextView
android:layout_width="111dp"
android:layout_height="wrap_content"
android:text="Condition"
android:id="@+id/textView2"
android:layout_weight="0.03"
android:textSize="20dp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.03"
android:id="@+id/rg1">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/newoption"
android:text="@string/New"
android:textSize="20dp"
android:onClick="Condition"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/usedoption"
android:text="@string/Used"
android:textSize="20dp"
android:onClick="Condition"
/>
</RadioGroup>
<TextView
android:layout_width="88dp"
android:layout_height="wrap_content"
android:text="Price"
android:id="@+id/textView3"
android:layout_weight="0.03"
android:textSize="20dp" />
<EditText
android:layout_width="184dp"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText" />
<Button
android:layout_width="117dp"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/submit"
android:layout_gravity="center_horizontal" />
</LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<ImageView
android:layout_width="437dp"
android:layout_height="200dp"
android:id="@+id/image_view"
android:layout_gravity="center_horizontal"
android:layout_weight="0.16" />
<TextView
android:layout_width="91dp"
android:layout_height="40dp"
android:text="Condition : "
android:id="@+id/textView4"
android:textSize="20dp" />
<TextView
android:layout_width="107dp"
android:layout_height="wrap_content"
android:text="Price : "
android:id="@+id/textView5"
android:layout_weight="0.16"
android:textSize="20dp" />
<Button
android:layout_width="138dp"
android:layout_height="wrap_content"
android:text="Finished"
android:id="@+id/button2"
android:layout_gravity="center_horizontal"
android:layout_weight="0.16"
android:textSize="20dp" />
</LinearLayout>
答案 0 :(得分:1)
出现问题是因为您没有正确设置RadioGroup
的监听器。
// First, set onCheckChangeListener into your RadioGroup
radio_g.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup rGroup, int checkedId)
{
// find view for both radio buttons
RadioButton rb1 = (RadioButton)rGroup.findViewById(R.id.newoption);
RadioButton rb2 = (RadioButton)rGroup.findViewById(R.id.usedoption);
if (rb1.isChecked()) {
SelectedType = rb1.getText().toString(); // set text into SelectedType
} else if (rb2.isChecked()) {
SelectedType = rb2.getText().toString();
}
}
});
答案 1 :(得分:0)
试试这个 包com.example.shiva_000.myproject;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import java.io.File;
import java.net.URI;
public class MainActivity extends Activity {
public static final String Tag_Price ="tag";
public static final String TAG_Radio ="Button";
private String SelectedType ="";
String path = "sdcard/camera_app/cam_image.jpg";
EditText et;
Button button;
ImageView imageView;
// Radio button
Button button2;
RadioGroup radio_g;
RadioButton rb1;
static final int cam_request = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button =(Button) findViewById(R.id.button1);
imageView =(ImageView) findViewById(R.id.image_view);
button2 = (Button) findViewById(R.id.submit);
et =(EditText) findViewById(R.id.editText);
radio_g=(RadioGroup) findViewById(R.id.rg1);
int selectedId=radio_g.getCheckedRadioButtonId();
rb1=(RadioButton)findViewById(selectedId);
SelectedType = rb1.getText().toString();
// on click for submit
button2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String s = et.getText().toString();
Intent i = new Intent(MainActivity.this,secondScreen.class);
i.putExtra("Condition",SelectedType);
i.putExtra("Price",s);
i.putExtra("image",path);
startActivity(i);
}
});
// on click for capture image
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent camera_Intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
camera_Intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(camera_Intent, cam_request);
}
});
}
private File getFile()
{
File folder = new File("sdcard/camera_app");
if (!folder.exists())
{
folder.mkdir();
}
File image_file = new File(folder,"cam_image.jpg");
return image_file;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// String path = "sdcard/camera_app/cam_image.jpg";
imageView.setImageDrawable(Drawable.createFromPath(path));
}
}