我的java文件为
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class ThemesActivity extends AppCompatActivity{
SharedPreferences sharedPreferences;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_themes);
}
@Override
protected void onStart() {
super.onStart();
setContentView(R.layout.activity_themes);
sharedPreferences = getApplicationContext().
getSharedPreferences("MyNumbers", Context.MODE_PRIVATE);
int bg = sharedPreferences.getInt("background_resource",
R.color.backYellow);
getWindow().setBackgroundDrawableResource(bg);
}
public void yellowClick(View view){
sharedPreferences = getApplicationContext().
getSharedPreferences("MyNumbers", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("background_resource", R.color.backYellow);
editor.putInt("button_resource", R.color.buttonYellow);
editor.apply();
getWindow().setBackgroundDrawableResource(R.color.backYellow);
}
public void redClick(View view){
sharedPreferences = getApplicationContext().
getSharedPreferences("MyNumbers", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("background_resource", R.color.backRed);
editor.putInt("button_resource", R.color.buttonRed);
editor.apply();
getWindow().setBackgroundDrawableResource(R.color.backRed);
}
public void blueClick(View view){
sharedPreferences = getApplicationContext().
getSharedPreferences("MyNumbers", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("background_resource", R.color.backBlue);
editor.putInt("button_resource", R.color.buttonBlue);
editor.apply();
getWindow().setBackgroundDrawableResource(R.color.backBlue);
}
public void greenClick(View view){
sharedPreferences = getApplicationContext().
getSharedPreferences("MyNumbers", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("background_resource", R.color.backGreen);
editor.putInt("button_resource", R.color.buttonGreen);
editor.apply();
getWindow().setBackgroundDrawableResource(R.color.backGreen);
}
}
资源文件为,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_light"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.dhritiman.any.ThemesActivity">
<!-- Login progress -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/linearLayout2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<Button
android:id="@+id/blueButton"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:background="@android:color/holo_blue_dark"
android:onClick="blueClick"
android:text="Blue"
android:visibility="visible" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<Button
android:id="@+id/yellowButton"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:background="@android:color/holo_orange_dark"
android:onClick="yellowClick"
android:text="Yellow"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_below="@+id/linearLayout2"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<Button
android:id="@+id/redButton"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:background="@android:color/holo_red_dark"
android:contextClickable="false"
android:linksClickable="false"
android:onClick="redClick"
android:text="Red"
android:visibility="visible" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<Button
android:id="@+id/greenButton"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:background="@android:color/holo_green_dark"
android:onClick="greenClick"
android:text="Green"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
请帮助,我的logcat显示进程指针0和进程指针1 我的意思是单击按钮时只进行触摸输入而不是按钮单击输入 我检查了一切,但没有找到问题。 提前致谢
答案 0 :(得分:0)
确保在创建活动时调用的onCreate()
内初始化视图和布局,当活动对用户可见时调用onStart()
。
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_themes);
// add here
sharedPreferences = getApplicationContext().getSharedPreferences("MyNumbers", Context.MODE_PRIVATE);
int bg = sharedPreferences.getInt("background_resource",R.color.backYellow);
getWindow().setBackgroundDrawableResource(bg);
}
@Override
protected void onStart() {
super.onStart();
// move to onCreate
}
答案 1 :(得分:0)
你的onclick可以正常工作只记录任何客户信息,但你只是在onCreate中初始化你的setContentView(R.layout.activity_themes),因为你的代码现在也工作但是init所有的视图组件应该在onCreate方法中初始化
谢谢!