如何使用editext类更改名称按钮

时间:2015-07-21 09:46:06

标签: java android android-spinner

我在Setup Activity中有editText。我使用SharedPreference来保存这个值,然后再使用它。我将此edittext值传递给另一个活动按钮文本。最初我需要隐藏按钮。如果edittext值来自sharedPreference,我需要显示button.Thanks alot

public class Setup extends Activity implements  AdapterView.OnItemSelectedListener {
EditText editText;
Button button;
Spinner spinner;
TextView text;
ArrayList<String> ar = new ArrayList<String>();
//    String a=editText.getText().toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setup);
    button = (Button) findViewById(R.id.button1);
    editText = (EditText) findViewById(R.id.ed1);
    spinner = (Spinner) findViewById(R.id.spinner1);
    text = (TextView) findViewById(R.id.back);
    getnameButton();
    ArrayAdapter<String> arr = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ar);
    arr.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
    spinner.setAdapter(arr);
    spinner.setOnItemSelectedListener(this);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences preferences = getSharedPreferences("sample", 0);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("Name",editText.getText().toString());
            editor.putInt("position",spinner.getSelectedItemPosition());
            editor.commit();
            Intent myIntent = new Intent(Setup.this, MainActivity.class);
            startActivity(myIntent);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_setup, menu);
    return true;
}

public void getnameButton() {
    Intent intent = getIntent();
    String text1 = intent.getStringExtra("text1");
    String text2 = intent.getStringExtra("text2");
    String text3 = intent.getStringExtra("text3");
    String text4 = intent.getStringExtra("text4");
    String text5 = intent.getStringExtra("text5");
    String text6 = intent.getStringExtra("text6");
    String text7 = intent.getStringExtra("text7");
    String text8 = intent.getStringExtra("text8");
    ar.add(text1);
    ar.add(text2);
    ar.add(text3);
    ar.add(text4);
    ar.add(text5);
    ar.add(text6);
    ar.add(text7);
    ar.add(text8);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    editText.setText(ar.get(position));
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}

=&gt;活动类

public class MainActivity extends Activity {

Button button1,button2,button3,button4,button5,button6,button7,button8;

TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button1=(Button) findViewById(R.id.btn1);
    button2=(Button) findViewById(R.id.btn2);
    button3=(Button) findViewById(R.id.btn3);
    button4=(Button) findViewById(R.id.btn4);
    button5=(Button) findViewById(R.id.btn5);
    button6=(Button) findViewById(R.id.btn6);
    button7=(Button) findViewById(R.id.btn7);
    button8=(Button) findViewById(R.id.btn8);
    textView=(TextView) findViewById(R.id.txtsua);
    getName();
    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(v.getContext(),Setup.class);
            intent.putExtra("text1", button1.getText().toString());
            intent.putExtra("text2",button2.getText().toString());
            intent.putExtra("text3",button3.getText().toString());
            intent.putExtra("text4", button4.getText().toString());
            intent.putExtra("text5",button5.getText().toString());
            intent.putExtra("text6",button6.getText().toString());
            intent.putExtra("text7", button7.getText().toString());
            intent.putExtra("text8", button8.getText().toString());
            startActivity(intent);
        }
    });

    // getnameButton();
}

public void getName() {
    button1.setVisibility(View.GONE);
    SharedPreferences preferences = getSharedPreferences("sample", 0);
    String Namestr = (preferences.getString("Name", ""));
    int position = (preferences.getInt("position", Integer.parseInt("")));
    for (int i = 0; i < 8; i++) {
        Button[] a = {button1, button2, button3, button4, button5, button6, button7, button8};
        if (Namestr.length() > 0&&position==i) {
            a[i].setVisibility(View.VISIBLE);
            a[i].setText(preferences.getString("Name", ""));
        }
        break;
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}}

0 个答案:

没有答案