安卓的onkeydown问题 [英] Android: OnKeyDown problem

查看:198
本文介绍了安卓的onkeydown问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的onkeydown处理后退按钮而是pressing后退按钮退出应用程序,而应该回到previous活动。以下是code我使用的:

 公共类NewsDetails延伸活动{/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    意向myIntent = getIntent();
    串ID = myIntent.getStringExtra(CONTENT_ID);
    字符串title = myIntent.getStringExtra(标题);
    字符串n标题=<字体颜色='白'>中+标题+< / FONT>中;
    的setContentView(R.layout.web);    super.onStart();
    最后弦乐mime类型=text / html的;
    最终字符串编码=UTF-8;    /*标题*/
    WV的WebView =(的WebView)findViewById(R.id.wv1);
    WebSettings webSettings = wv.getSettings();
    webSettings.setDefaultFontSize(15);
    wv.setBackgroundColor(Color.BLACK);
    wv.loadData(n标题,mime类型,编码);    /*身体*/
    字符串XML = XMLfunctions.getBodyXML(ID);
    字符串结果= xml.replaceAll(&所述; P>中,&所述p为H.;&下;股利对齐= \\证明\\>中);
    串nXml = result.replaceAll(&下; / P>中,&所述; / DIV>&下; / P>中);
    字符串nBody =<字体颜色='白'>中+ nXml +< / FONT>中;
    的WebView WV1 =(的WebView)findViewById(R.id.wv2);
    wv1.setBackgroundColor(Color.BLACK);
    WebSettings webSettings1 = wv1.getSettings();
    webSettings1.setDefaultFontSize(10);
    wv1.loadData(nBody,mime类型,编码);}@覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){
    如果((键code == KeyEvent.KEY code_BACK)){
        意向意图=新意图(NewsDetails.this,TopNewsActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        查看查看= TopNewsGroup.group.getLocalActivityManager()startActivity(ShowNews,意图).getDecorView()。
        TopNewsGroup.group.setContentView(视图);
        返回true;
    }
    返回super.onKeyDown(键code,事件);}公共布尔onCreateOptionsMenu(菜单菜单){    super.onCreateOptionsMenu(菜单);
    MenuInflater吹气= getMenuInflater();
    inflater.inflate(R.menu.optionsmenu,菜单);
    返回true;
}公共布尔onOptionsItemSelected(菜单项项){    开关(item.getItemId()){        案例R.id.refresh:
                startActivity(新意图(这一点,NewsDetails.class));
                返回true;        案例R.id.search:
                startActivity(新意图(这一点,SearchActivity.class));
                返回true;        案例R.id.info:
                startActivity(新意图(这一点,NewsDetails.class));
                返回true;        案例R.id.exit:
                完();
                返回true;
    }
  返回false;
}}


解决方案

由于我使用的ActivityGroup我没有使用历史堆栈,但使用

 公共无效回(){
    如果(history.size()大于0){
        history.remove(history.size() - 1);
        如果(history.size()&下; = 0){
            完();
        }其他{
            的setContentView(history.get(history.size() - 1));
        }
    }其他{
        完();
    }
}@覆盖
公共无效onBack pressed(){
    TopNewsGroup.group.back();
}

我能回迁previous活动。

I am using onKeyDown for handling the back button but on pressing the back button the application exits whereas it should go back to the previous activity. Following is the code i'm using:

public class NewsDetails extends Activity{

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent myIntent = getIntent(); 
    String id = myIntent.getStringExtra("content_id");
    String title = myIntent.getStringExtra("title");
    String nTitle = "<font color='white'>"+title+"</font>";
    setContentView(R.layout.web);

    super.onStart();
    final String mimeType = "text/html";
    final String encoding = "utf-8";

    /*Title*/
    WebView wv = (WebView)findViewById(R.id.wv1);
    WebSettings webSettings = wv.getSettings();
    webSettings.setDefaultFontSize(15);
    wv.setBackgroundColor(Color.BLACK);
    wv.loadData(nTitle, mimeType, encoding);

    /*Body*/
    String xml = XMLfunctions.getBodyXML(id);
    String result = xml.replaceAll("<p>", "<p><div align=\"justify\">");
    String nXml = result.replaceAll("</p>", "</div></p>");
    String nBody = "<font color='white'>"+nXml+"</font>" ;
    WebView wv1 = (WebView)findViewById(R.id.wv2);
    wv1.setBackgroundColor(Color.BLACK);
    WebSettings webSettings1 = wv1.getSettings();
    webSettings1.setDefaultFontSize(10);
    wv1.loadData(nBody, mimeType, encoding);

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        Intent intent = new Intent(NewsDetails.this, TopNewsActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        View view = TopNewsGroup.group.getLocalActivityManager().startActivity("ShowNews", intent).getDecorView();
        TopNewsGroup.group.setContentView(view);
        return true;
    }
    return super.onKeyDown(keyCode, event); 

}

public boolean onCreateOptionsMenu(Menu menu) {

    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.optionsmenu, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item){

    switch (item.getItemId()){

        case R.id.refresh:
                startActivity(new Intent(this, NewsDetails.class));
                return true;

        case R.id.search:
                startActivity(new Intent(this, SearchActivity.class));
                return true;

        case R.id.info:
                startActivity(new Intent(this, NewsDetails.class));
                return true;

        case R.id.exit:
                finish();
                return true;
    }
  return false;
}



}

解决方案

As i'm using ActivityGroup i wasn't using the history stack but on using

public void back() {  
    if(history.size() > 0) {  
        history.remove(history.size()-1);  
        if(history.size()<=0){
            finish();
        }else{
            setContentView(history.get(history.size()-1));
        } 
    }else {  
        finish();  
    }  
}

@Override  
public void onBackPressed() {  
    TopNewsGroup.group.back();
}

i was able to move back to the previous activity.

这篇关于安卓的onkeydown问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆