千涯秋瑟 2013-11-04 15:00 采纳率: 0%
浏览 961

安卓开发刚起步,控制UI界面时 出现这个问题怎么解决啊 如图

CSDN移动问答
CSDN移动问答
MainActivity。java 代码如下:
package com.example.andrew;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
public TextView text2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frameLayout=new FrameLayout(this);
frameLayout.setBackgroundDrawable(this.getResources().
getDrawable(TRIM_MEMORY_UI_HIDDEN));//设置背景
setContentView(frameLayout);
TextView text1=new TextView(this);
text1.setText("在代码中控制UI界面");
text1.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24);
text1.setTextColor(Color.CYAN);
frameLayout.addView(text1);

text2=new TextView(this);
text2.setText("单击进入游戏界面。。。。");
text2.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24);
text2.setTextColor(Color.GREEN);
LayoutParams params=new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity=Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL;
text2.setLayoutParams(params);
text2.setOnClickListener(new OnClickListener(){
@ Override
public void onClick(View v){
new AlertDialog.Builder(MainActivity.this).setTitle("系统提示")
.setMessage("游戏有风险")
.setNegativeButton("确定",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.i("android","进入游戏");

}
}).setNegativeButton("退出",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.i("android", "退出游戏");
finish();
}
}).show();
}
});

frameLayout.addView(text2);

}

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

}

  • 写回答

1条回答

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2024-02-13 17:06
    关注

    所有内容均由人工智能模型输出,其内容的准确性和完整性无法保证,不代表我的态度或观点。


    该回答引用文心大模型4.0,仅提供参考,不保证完全正确
    目前为文心大模型4.0专栏,网页版本:V2.5.3(2024.1.19)
    首先,我看到你在frameLayout.setBackgroundDrawable(this.getResources().getDrawable(TRIM_MEMORY_UI_HIDDEN));这行代码中使用了一个未定义的常量TRIM_MEMORY_UI_HIDDEN,这个常量应该是你自定义的,但是在你的代码中并没有看到这个常量的定义,你需要替换这个常量为Android系统中已经定义的资源ID,或者替换为你自己项目中定义的资源ID。


    其次,在AlertDialog的设置中,你用了两次setNegativeButton(),这会导致只有第二个按钮会生效,第一个按钮的设置会被覆盖。你应该使用setPositiveButton()来设置第二个按钮,这样两个按钮的设置都会生效。


    下面是修改后的代码:

    package com.example.andrew;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.graphics.Color;
    import android.util.Log;
    import android.util.TypedValue;
    import android.view.Gravity;
    import android.view.Menu;
    import android.view.ViewGroup;
    import android.widget.FrameLayout;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
        public TextView text2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            FrameLayout frameLayout = new FrameLayout(this);
            // 请将TRIM_MEMORY_UI_HIDDEN替换为有效的资源ID,或者删除这行代码
            // frameLayout.setBackgroundDrawable(this.getResources().getDrawable(TRIM_MEMORY_UI_HIDDEN));
            setContentView(frameLayout);
    
            TextView text1 = new TextView(this);
            text1.setText("在代码中控制UI界面");
            text1.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24);
            text1.setTextColor(Color.CYAN);
            frameLayout.addView(text1);
    
            text2 = new TextView(this);
            text2.setText("单击进入游戏界面。。。。");
            text2.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24);
            text2.setTextColor(Color.GREEN);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
            text2.setLayoutParams(params);
            text2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new AlertDialog.Builder(MainActivity.this).setTitle("系统提示")
                            .setMessage("游戏有风险")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Log.i("android", "进入游戏");
                                }
                            })
                            .setNegativeButton("退出", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Log.i("android", "退出游戏");
                                    finish();
                                }
                            }).show();
                }
            });
    
            frameLayout.addView(text2);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    }
    

    另外,请注意,我已经将import android.app.ActionBar.LayoutParams;更改为import android.widget.FrameLayout.LayoutParams;,因为你正在使用FrameLayout,所以应该使用FrameLayout的LayoutParams。同时,我也已经删除了import android.annotation.SuppressLint;import android.view.View.OnClickListener;,因为这两行代码在你的代码中并没有用到。


    最后,你的代码中存在一些格式问题,我已经在修改后的代码中进行了调整。在编写代码时,请注意代码的格式和缩进,这样可以提高代码的可读性。


    图片转文字可能造成误差,不代表模型真实水平

    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示