【问题标题】:getText from one layout and (setting it) setText in an other layoutgetText 从一个布局和(设置它) setText 在另一个布局
【发布时间】:2012-11-24 11:51:17
【问题描述】:

我从带有主布局的 mainAcitivity 调用此方法 SolveUpdation(来自 button-onclickAction 监听器)。我使用其他布局从用户那里获取价值并将其设置为主布局中的按钮标题,这只是对我不起作用的指令

    private void  SolveUpdation() {     //this function call is generated from the main Activity with main layout       
        setContentView(R.layout.updateappliance); //this is 2nd layout to get values from user and use them as buttonText in the main layout   

        btnSaveApp = (Button) findViewById(R.id.Bupdatenow);
        btnSaveApp.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {       
                // TODO Auto-generated method stub
                  mOutEditText = (EditText) findViewById(R.id.edit_text_1);               
                  TextView view1 = (TextView) findViewById(R.id.edit_text_1);
                  final String TitleApp1 = view1.getText().toString();  //the value is read properly here

//                if (App1.length() > 0) {
//                    byte[] send = App1.getBytes();
//                }

                btnSaveApp.setText(TitleApp1); //this works fine
                startActivity(new Intent(HomeScreen.this, HomeScreen.class));//this the main activity for main layout
                setContentView(R.layout.main); //this is the main layout and this instruction works
                buttonLED1.setText(TitleApp1); //buttonLED1 (a Togglebutton or can be simple) is  defined in main layout and this does not works and this is what i am stuck with
                SaveAppNamesToast(TitleApp1);   //this is just to toast the value and it works fine.
            }});

所以请谁能指导我为什么这个指令 buttonLED1.setText(TitleApp1);不工作???任何帮助将不胜感激......谢谢

【问题讨论】:

    标签: android-layout gettext settext


    【解决方案1】:

    无意冒犯,但您编写代码的方式并不是一个好习惯。 我的建议:停止在主活动中调用另一个 setContentView。您应该在一个布局中实现所有需要的 Button 和 EditText,并将它们的可见性设置为 gonevisible,具体取决于单击的按钮。

    如果您不想这样做,您应该创建第二个类来处理用户的输入。按下保存按钮后,您初始化主活动的意图并通过intent.putExtra("KEY", value) 提供用户输入。 您的 Main Activity 可以通过 getIntent().getExtras().getInt("KEY") 接收此值。

    顺便说一句:我认为您当前的代码无法正常工作,因为您启动了新的 Activity。通过这个,一切都会再次初始化,所以你看到的 buttonLED1 与获取文本的 buttonLED1 不同。

    【讨论】:

    • 我猜你说的方式是我想到的,但我不知道这些特定的关键字,因为我是初学者..让我试试这个......你太棒了......谢谢老兄..
    猜你喜欢
    • 2018-09-11
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 2013-11-23
    相关资源
    最近更新 更多