0

mainAcitivityからメインレイアウトでこのメソッドSolveUpdation(button-onclickAction Listenerから)を呼び出します。私は他のレイアウトを使用してユーザーから値を取得し、それをメインレイアウトのボタンタイトルとして設定しますが、それは私にとっては機能しない唯一の指示です

    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.
            }});

したがって、plzは、なぜこの命令buttonLED1.setText(TitleApp1);を私に導くことができます。動作しません??? どんな助けでも理解できるでしょう..ありがとう

4

1 に答える 1

0

不快感はありませんが、コードの書き方は良い習慣ではありません。私のアドバイス:setContentViewメインアクティビティで別の人に電話するのはやめましょう。むしろ、必要なすべてのボタンとEditTextを1つのレイアウトに実装し、クリックされたボタンに応じて、goneまたはそれらの表示を設定する必要があります。visible

これを行いたくない場合は、ユーザーの入力を処理する2番目のクラスを作成する必要があります。保存ボタンを押した後、メインアクティビティのインテントを初期化しintent.putExtra("KEY", value)、ユーザーの入力を介してそれを与えます。メインアクティビティは、を介してこの値を受け取ることができますgetIntent().getExtras().getInt("KEY")

ちなみに、新しいアクティビティを開始したため、現在のコードは機能しないと思います。これにより、すべてが再び初期化されるため、表示されるものはテキストを取得するものbuttonLED1と同じではありません。buttonLED1

于 2012-11-24T12:32:57.133 に答える