简体   繁体   English

当我单击MainActivity中的按钮以显示其他活动时,它不起作用

[英]It doesn't work when i click on a button in MainActivity in order to show other activity

Please I'm trying to create an activity with 6 buttons, and 6 textviews. 请尝试用6个按钮和6个textview创建一个活动。 Note that the problem gone when I click the button because it works when I tried to comment on block my code and just let this: 请注意,当我单击按钮时问题就消失了,因为当我尝试对代码块进行注释并允许这样做时,该问题就起作用了:

 super.onCreate(savedInstanceState);

 setContentView(R.layout.tasbih);

Her is my activity if you understand were the problem remains please help me: 她是我的活动,如果您了解问题仍然存在,请帮助我:

public class Tasbih extends AppCompatActivity {
private TextView textView, textView2, textView6;
private Button button7, button8, button9, button10, button11, button12;
private int j = 0;
private int k = 0;
private int l = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.tasbih);

 textView=(TextView) findViewById(R.id.textView);
    textView2=(TextView) findViewById(R.id.textView2);
    textView6=(TextView) findViewById(R.id.textView6);
    button7=(Button)findViewById(R.id.button7);
    button8=(Button)findViewById(R.id.button8);
    button9=(Button)findViewById(R.id.button9);
    button10=(Button)findViewById(R.id.button10);
    button11=(Button)findViewById(R.id.button11);
    button12=(Button)findViewById(R.id.button12);
    button7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            j++;
            textView.setText(j);
        }
    }) ;
    button8.setOnClickListener(new View.OnClickListener() {
                                   @Override
                                   public void onClick(View v) {
                                       j=0;
                                       textView.setText(j);
                                   }
                               });
            button9.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    k++;
                    textView2.setText(k);
                }
            });
    button10.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            k=0;
            textView2.setText(k);
        }
    }) ;
    button11.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            l++;
            textView6.setText(l);
        }
    }) ;
    button12.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            l=0;
            textView6.setText(l);
        }
    }) ;

    final SharedPreferences sp= 
  PreferenceManager.getDefaultSharedPreferences(this);
    j = sp.getInt("j", 0);
    k = sp.getInt("k", 0);
    l = sp.getInt("l", 0);
    textView.setText(j);
    textView2.setText(k);
    textView6.setText(l);

    textView.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, 
          int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int 
            count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            SharedPreferences.Editor editor = sp.edit();
            editor.putInt("j", j).apply();
        }
    });



    textView2.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, 
   int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            SharedPreferences.Editor editor = sp.edit();
            editor.putInt("k", k).apply();
        }
    });



    textView6.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            SharedPreferences.Editor editor = sp.edit();
            editor.putInt("l", l).apply();
        }
    });
     }

@Override
protected void onPause() {
    super.onPause();

   /* SharedPreferences sp = getSharedPreferences("tasbih1", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    editor.putInt("j", j);
    editor.putInt("k", k);
    editor.putInt("l", l);
    editor.commit();*/

}

 } 

Where is the problem ?! 问题出在哪儿 ?!

You can open other activity from current activity by intent. 您可以按意图从当前活动中打开其他活动。 Like this. 像这样。

You can add this line in your button click listner. 您可以在按钮单击列表器中添加此行。 Replace OtherActivity.class name to the class name of your other activity. 将OtherActivity.class名称替换为其他活动的类名称。

startActivity(new Intent(Tasbih.this,OtherActivity.class));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 片段未显示在Mainactivity中,在我的活动片段内容仅在我单击工具栏时显示 - Fragment doesn't show in Mainactivity, In my activity fragment content only show when I click on toolbar 当我单击按钮时,它不起作用(Android) - When I click the Button it doesn't work (Android) 我想在单击按钮1时打印数字1,但它不起作用 - I want to print number 1 when click the button 1 but it doesn't work 当我单击位于顶视图活动中的按钮并且 MainActivity 未处于活动状态时,剪贴板返回 null - Clipboard returns null when I click on the button located in the top view activity and when MainActivity is not active Android Mainactivity不显示activity_main.xml - Android Mainactivity doesn't show activity_main.xml 单击它时,我的第二个活动布局无法启动 - My second activity layout doesn't start when I click it 当我单击我的 JButton 时,JLabel 不会显示在 window 上, - When I click my JButton, the JLabel doesn't show on the window, 我在一项活动中有 2 个按钮。 当我点击它们时,我会创建新的意图,这样我就可以创建一个新的屏幕。 一个有效,另一个无效 - I have 2 buttons in one activity. When I click on them, I make new intents so I can make a new screen. One works, the other doesn't 我的布局未显示在MainActivity上 - My layout doesn't show on my MainActivity 当我单击按钮时,没有出现JFreeChart - JFreeChart doesn't appear when I click on button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM