menu=>main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_prefs"
android:title="@string/menu_prefs"
android:icon="@android:drawable/ic_menu_preferences"></item>
<item
android:id="@+id/server8"
android:title="开始"></item>
<item
android:id="@+id/OpenShowView"
android:title="显示更新"/>
</menu>
MainActivity.java
package com.example.countinput;
import android.os.Binder;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.SimpleCursorAdapter.ViewBinder;
import android.widget.TextView;
public class MainActivity extends Activity implements TextWatcher,OnSharedPreferenceChangeListener {
private SharedPreferences prefs;
private TextView text_count;
private EditText input;
private Count_Application ca;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater Inflater = this.getMenuInflater();
Inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.menu_prefs:
this.startActivity(new Intent(this,PrefsActivity.class));
break;
case R.id.server8:
if(ca.GetServer()){
this.startService(new Intent(this,Update_Server.class));
}else{
this.stopService(new Intent(this,Update_Server.class));
}
break;
case R.id.OpenShowView:
this.startActivity(new Intent(this,ShowActivity.class).addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT));
break;
}
return true;
}
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
// TODO Auto-generated method stub
menu.findItem(R.id.server8);
return super.onMenuOpened(featureId, menu);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text_count = (TextView)this.findViewById(R.id.counts);
input = (EditText)this.findViewById(R.id.eidts);
input.addTextChangedListener(this);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
ca = (Count_Application)this.getApplication();
//DBHelper dbs = new DBHelper(this);
//SQLiteDatabase db = dbs.getReadableDatabase();
//input.setText(prefs.getString("UserName", ""));
}
;
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
int text_lenght = 140 -s.length();
//this.text_count.setText(String.valueOf(text_lenght));
this.text_count.setTextColor(Color.GREEN);
if(text_lenght<10){
this.text_count.setTextColor(Color.YELLOW);
}
if(text_lenght<0){
this.text_count.setTextColor(Color.RED);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
//input.setText(prefs.getString("UserName", ""));
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
// TODO Auto-generated method stub
//input.setText(prefs.getString("UserName", ""));
Count_Application apps = (Count_Application)this.getApplication();
input.setText(apps.getCount());
}
}
1 个解决方案
#1
不要在onMenuOpened方法里面修改title,就在onCreateOptionsMenu里面修改。所以这里可以不用重写onMenuOpened方法
#1
不要在onMenuOpened方法里面修改title,就在onCreateOptionsMenu里面修改。所以这里可以不用重写onMenuOpened方法