【问题标题】:how to use single variable to pass selected imageview value to another activity?如何使用单个变量将选定的 imageview 值传递给另一个活动?
【发布时间】:2013-08-02 06:50:24
【问题描述】:

在下面的代码中,我将 txt1 txt2 txt3 名称作为参数传递给另一个活动,并获得三个时间变量 param1、param2、param3。而不是这些,how do I use just a single variable which saves the value of press imageview?Can I get just a single variable which shows the press button value?它不起作用,但如果我按早餐是值 iwllb 粗体但如果我按晚餐或午餐 textview 不是粗体告诉我为什么???

   public class thirdstep extends Activity {
        ImageButton imgNews, imgContact, imgSetting;
        TextView txt1, txt2, txt3;
        private ImageLoader2 imgLoader;
        String MenuSelect;

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

            ImageView img1 = (ImageView) findViewById(R.id.img1);
            txt1 = (TextView) findViewById(R.id.txt1);

            ImageView img2 = (ImageView) findViewById(R.id.img2);
            txt2 = (TextView) findViewById(R.id.txt2);

            ImageView img3 = (ImageView) findViewById(R.id.img3);
            txt3 = (TextView) findViewById(R.id.txt3);

            MenuSelect = Utils.MenuSelect;
            imgLoader = new ImageLoader2(this);

            try {

                HttpClient client = new DefaultHttpClient();
                HttpConnectionParams
                        .setConnectionTimeout(client.getParams(), 15000);
                HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
                HttpUriRequest request = new HttpGet(MenuSelect);
                HttpResponse response = client.execute(request);
                InputStream atomInputStream = response.getEntity().getContent();
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        atomInputStream));

                String line;
                String str = "";
                while ((line = in.readLine()) != null) {
                    str += line;
                }

                JSONObject json = new JSONObject(str);
                JSONArray data = json.getJSONArray("worldpopulation");
                JSONObject jsonOrder = (JSONObject) data.get(0);

                txt1.setText(jsonOrder.getString("name"));
                String url1 = jsonOrder.getString("url");
                imgLoader.DisplayImage(url1, img1);

                JSONObject jsonOrder2 = (JSONObject) data.get(1);
                txt2.setText(jsonOrder2.getString("name2"));
                String url2 = jsonOrder2.getString("url2");
                imgLoader.DisplayImage(url2, img2);

                JSONObject jsonOrder3 = (JSONObject) data.get(2);
                txt3.setText(jsonOrder3.getString("name3"));
                String url3 = jsonOrder3.getString("url3");
                imgLoader.DisplayImage(url3, img3);

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block

                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            img1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent iMenuList = new Intent(thirdstep.this,
                    fourthscreen.class);

            iMenuList.putExtra("Menuitem", txt1.getText().toString());

            startActivity(iMenuList);

        }
    });

    img2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent iMenuList = new Intent(thirdstep.this,
                    fourthscreen.class);

            iMenuList.putExtra("Menuitem", txt2.getText().toString());

            startActivity(iMenuList);

        }
    });

    img3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent iMenuList = new Intent(thirdstep.this,
                    fourthscreen.class);

            iMenuList.putExtra("Menuitem", txt3.getText().toString());

            startActivity(iMenuList);

        }
    });

}

 }


            public class fourthscreen extends Activity 

            Bundle bundle = this.getIntent().getExtras();
            String param1 = bundle.getString("Breakfast");
            String param2 = bundle.getString("Lunch");
            String param3 = bundle.getString("Supper");

Is it possible i just used single variable?

   String param1 = bundle.getString("Presimageview value???");





                         if(param1.equalsIgnoreCase("BreakFast"))
     {
         Breakfast.setTypeface(null, Typeface.BOLD);
     }
     else 
         if(param1.equalsIgnoreCase("Lunch"))
         {
             Lunch.setTypeface(null, Typeface.BOLD);
         }
         else 
             if(param1.equalsIgnoreCase("Supper"))
             {
                 Lunch.setTypeface(null, Typeface.BOLD);
             }


     i did using this code  iMenuList.putExtra("Menuitem", 
   txt3.getText().toString());   iswork now
 //textview value not bold on lunch and supper only brekafast value wll bold

【问题讨论】:

  • 我不想在第 3 次的行 String param1 = bundle.getString("Breakfast");我可以只使用单一方法来获取新闻图像视图值的名称吗????
  • 我只想在第四屏开始时显示按下的按钮值,如果我按下 img1,toast 将显示“breakFast”,如果我按下 img2 按钮,Toast 将显示午餐...

标签: android android-intent bundle


【解决方案1】:

只需将单行放在每次图像点击的点击上。

iMenuList.putExtra("LUNCH_TYPE", txt.getText().toString());

而不是

iMenuList.putExtra("Breakfast", txt1.getText().toString());

iMenuList.putExtra("Lunch", txt2.getText().toString());

iMenuList.putExtra("Supper", txt3.getText().toString());

在第四屏活动中只写单行

String strLunchType  = this.getIntent().getStringExtras("LUNCH_TYPE");

【讨论】:

  • 请在第二个屏幕上检查文本视图而不是粗体
【解决方案2】:

不清楚您在寻找什么。但是,看看这是不是你想要的:

img1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Intent iMenuList = new Intent(thirdstep.this,
                fourthscreen.class);

        iMenuList.putExtra("Choice", "BreakFast");

        startActivity(iMenuList);

    }
});

img2.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Intent iMenuList = new Intent(thirdstep.this,
                fourthscreen.class);

        iMenuList.putExtra("Choice", "Lunch");

        startActivity(iMenuList);

    }
});

img3.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Intent iMenuList = new Intent(thirdstep.this,
                fourthscreen.class);

        iMenuList.putExtra("Choice", "Supper");

        startActivity(iMenuList);

    }
});

在您的fourthscreen 活动中:

String param = bundle.getStringExtra("Choice");

Toast.makeText(this, param, Toast.LENGTH_LONG).show();

【讨论】:

    【解决方案3】:

    为什么不在每个 OnClickListener 中使用相同的 Extra 名称?:

      iMenuList.putExtra("Food", txt<b>N</b>.getText().toString());

    然后在你的第四屏中额外阅读“食物”:

      String param = bundle.getString("Food");

    【讨论】:

    • is work thnx just help me mpore i update my code textview not bold when value is lunch or supper 为什么?
    • 我想加粗选定的文本视图,但不工作,为什么?我用了if条件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多