字符串未在第二个活动中进行比较

时间:2017-12-29 10:15:45

标签: android

我需要来自Mainactivity的EditText中的字符串,以便我可以比较该值并在下一个显示所需的图像。但只有else部分在第二个活动中工作。我尝试过这段代码,但确实没有用。

private Button b1;
static EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et = (EditText)findViewById(R.id.pass);
    b1 = (Button)findViewById(R.id.clickhere);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(et.getText().toString().equals(getString(R.string.Ronnie)))
            {
                Intent myIntent = new Intent(MainActivity.this, 
        Thought.class);
                startActivity(myIntent);


            }
            else if(et.getText().toString().equals(getString(R.string.Ankita)))
            {
                Intent myIntent = new Intent(MainActivity.this, Thought.class);
                startActivity(myIntent);

            }

            else
            {
                Toast.makeText(getApplicationContext(),"Not for you",Toast.LENGTH_SHORT);
            }

        }
    });
}

和第二个活动代码

public class Thought extends MainActivity {

public ImageView iv;
static String s1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent=getIntent();
    setContentView(R.layout.activity_thought);
    s1 = MainActivity.et.getText().toString();
    iv = (ImageView)findViewById(R.id.imageView);
    if(s1.equals(getString(R.string.Ronnie)))
    {
        iv.setImageResource(R.drawable.ronniel);
    }
    else if(s1.equals(getString(R.string.Ankita)))
    {
        iv.setImageResource(R.drawable.ankitat);
    }
    else
    {
        iv.setImageResource(R.drawable.subha);
    }
}
}

4 个答案:

答案 0 :(得分:0)

尝试此代码:在主要活动中

Intent myIntent = new Intent(MainActivity.this, Thought.class);
myIntent.putstring("title",et.getText().toString());
startActivity(myIntent);

然后在think.class中使用:

s1 = getIntent().getstring("title");

并使用此代替s1 = MainActivity.et.getText().toString();

答案 1 :(得分:0)

MainActivity

    b1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(et.getText().toString().equals(getString(R.string.Ronnie)))
        {
            Intent myIntent = new Intent(MainActivity.this, 
        Thought.class);
             myIntent.putExtra("MyEditText",et.getText().toString);
            startActivity(myIntent);


        }
        else if(et.getText().toString().equals(getString(R.string.Ankita)))
        {
            Intent myIntent = new Intent(MainActivity.this, Thought.class);
              myIntent.putExtra("MyEditText",et.getText().toString);
            startActivity(myIntent);

        }

        else
        {
            Toast.makeText(getApplicationContext(),"Not for you",Toast.LENGTH_SHORT);
        }

    }
});

SecondActivity

 s1 = getIntent().getStringExtra("MyEditText")

答案 2 :(得分:0)

您可以将edittext值从一个活动传递到另一个活动。通过这种方式,您可以避免编辑文本值来比较第二个活动的代码。

这里我添加了示例代码

第一项活动

Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("editTextValue", et.getText().toString());
startActivity(intent);

第二项活动

if(getIntent()!=null){
   String editTextValue = getIntent().getStringExtra("editTextValue");
}

并像这样更改您的代码。实际上,这是将一个值从一个活动传递给另一个活动的正确方法。

        if(editTextValue.equals(getString(R.string.Ronnie)))
        {
            iv.setImageResource(R.drawable.ronniel);
        }

答案 3 :(得分:0)

使用Bundle在活动之间传递Strings

private Button b1;
static EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et = (EditText)findViewById(R.id.pass);
    b1 = (Button)findViewById(R.id.clickhere);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String pass = et.getText().toString();
            if(pass.equals(getString(R.string.Ronnie)) || pass.equals(getString(R.string.Ankita)))
            {
                Intent myIntent = new Intent(MainActivity.this, Thought.class);
                myIntent.putExtra("pass",pass);
                startActivity(myIntent);


            }else{
                Toast.makeText(getApplicationContext(),"Not for you",Toast.LENGTH_SHORT);
            }

        }
    });
}

获取类似

的数据
public class Thought extends Activity {

public ImageView iv;
static String s1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_thought);

    iv = (ImageView)findViewById(R.id.imageView);

    final Intent intent = getIntent();
    Bundle bundle = intent.getExtras();

    if(bundle != null){
       String pass = bundle.getString("pass");
             if(pass.equals(getString(R.string.Ronnie)))
              {
                 iv.setImageResource(R.drawable.ronniel);
              }
             else if(pass.equals(getString(R.string.Ankita)))
             {
                 iv.setImageResource(R.drawable.ankitat);
             }

    }else
    {
        iv.setImageResource(R.drawable.subha);
    }
}
}