使用Switch显示输出

时间:2017-05-13 03:06:28

标签: android colors switch-statement conditional

我正在将项目从按钮升级到切换代码。然而,它正在建设中,但是当我进入Main时它会崩溃。

所以基本上我有4个颜色的4个按钮。我为.xml中的每个“开”和“关”状态设置了文本。然后我去了Main并用条件语句检查每个开关的状态。例如,如果红色和蓝色亮起,则输出(“问候语”)将显示为:“状态为:红色蓝色”(因为绿色和黄色是空格)

有人能发现错误吗?事件日志很干净,所以我不知道问题是什么。

package app.real_time_chat;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.view.View.OnClickListener;


import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by David on 5/01/2017
 */
public class Chat_Room  extends AppCompatActivity{

private Button btn_send_msg;
private Button button_msg;
private EditText input_msg;
private TextView chat_conversation;

Switch button_red, button_blue, button_green, button_yellow;

private String user_name,room_name;
private DatabaseReference root ;
private String temp_key;
private String greeting;                                                                    
//for displaying button messages

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chat_room);


    // initiate view's
    button_red = (Switch) findViewById(R.id.button_red);
    button_blue = (Switch) findViewById(R.id.button_blue);
    button_green = (Switch) findViewById(R.id.button_green);
    button_yellow = (Switch) findViewById(R.id.button_yellow);

    btn_send_msg = (Button) findViewById(R.id.btn_send);                                   
 //for original message sending program

    btn_send_msg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String statusSwitch_Red, statusSwitch_Blue, statusSwitch_Green, 
 statusSwitch_Yellow;
            if (button_red.isChecked())
                statusSwitch_Red = button_red.getTextOn().toString();
            else
                statusSwitch_Red = button_red.getTextOff().toString();
            if (button_blue.isChecked())
                statusSwitch_Blue = button_blue.getTextOn().toString();
            else
                statusSwitch_Blue = button_blue.getTextOff().toString();
            if (button_green.isChecked())
                statusSwitch_Green = button_green.getTextOn().toString();
            else
                statusSwitch_Green = button_green.getTextOff().toString();
            if (button_yellow.isChecked())
                statusSwitch_Yellow = button_yellow.getTextOn().toString();
            else
                statusSwitch_Yellow = button_yellow.getTextOff().toString();
            greeting = "Status is: " + statusSwitch_Red + "/n" + 
statusSwitch_Blue + "/n" + statusSwitch_Green + "/n" + statusSwitch_Yellow;  
// display the current state for switch's
        }
    });



    input_msg = (EditText) findViewById(R.id.msg_input);
    chat_conversation = (TextView) findViewById(R.id.textView);

    user_name = getIntent().getExtras().get("user_name").toString();                
//converts username to string to display to screen
    room_name = getIntent().getExtras().get("room_name").toString();                
//converts room name to string to display to screen
    setTitle(" Room - "+room_name);                                                 
//displays pre-set room name to top of app page

    root = FirebaseDatabase.getInstance().getReference().child(room_name);



    btn_send_msg.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {




            Map<String,Object> map = new HashMap<String, Object>();
            temp_key = root.push().getKey();
            root.updateChildren(map);

            DatabaseReference message_root = root.child(temp_key);
            Map<String,Object> map2 = new HashMap<String, Object>();
            map2.put("name",user_name);
            map2.put("msg",greeting);
            //             map2.put("msg",input_msg.getText().toString());                           
for original message sending program


            message_root.updateChildren(map2);
        }

    });

    root.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            append_chat_conversation(dataSnapshot);
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            append_chat_conversation(dataSnapshot);

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

private String chat_msg,chat_user_name;

private void append_chat_conversation(DataSnapshot dataSnapshot) {

    Iterator i = dataSnapshot.getChildren().iterator();

    while (i.hasNext()){

           chat_msg = (String) ((DataSnapshot)i.next()).getValue();
        chat_user_name = (String) ((DataSnapshot)i.next()).getValue();

        chat_conversation.append(chat_user_name +" : "+chat_msg +" \n");
    }


}
}

这是.xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="@color/pale_blue"
android:layout_height="match_parent"
android:id="@+id/linearlayout">

<ImageView
    android:layout_width="382dp"
    android:layout_height="62dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:src="@drawable/alumis_logo"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_editor_absoluteY="-4dp"
    app:layout_constraintHorizontal_bias="0.454"
    android:id="@+id/imageView"
     />

 <Switch
    android:id="@+id/button_red"
    android:layout_width="99dp"
    android:layout_height="30dp"
    app:layout_constraintBottom_toTopOf="@+id/button_blue"
    app:layout_constraintTop_toTopOf="parent"
    android:text="   Red"
    android:background="#ff0000"
    android:layout_marginTop="60dp"
    android:layout_marginBottom="1dp"
    android:layout_marginRight="8dp"
    app:layout_constraintVertical_bias="0.484"
    app:layout_constraintRight_toRightOf="parent"
    android:textOn="赤色"
    android:textOff=" " />

<Switch
    android:id="@+id/button_blue"
    android:layout_width="99dp"
    android:layout_height="30dp"
    app:layout_constraintBottom_toTopOf="@+id/button_green"
    app:layout_constraintTop_toBottomOf="@+id/button_red"
    android:text="   Blue"
    android:background="#0000ff"
    android:layout_marginBottom="1dp"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="1dp"
    tools:layout_editor_absoluteX="279dp"
    android:textOn="青色"
    android:textOff=" " />

<Switch
    android:id="@+id/button_green"
    android:layout_width="99dp"
    android:layout_height="30dp"
    app:layout_constraintBottom_toTopOf="@+id/button_yellow"
    app:layout_constraintTop_toBottomOf="@+id/button_blue"
    android:text="   Green"
    android:background="#00ff00"
    android:layout_marginBottom="1dp"
    android:layout_marginTop="1dp"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:textOn="緑色"
    android:textOff=" " />

<Switch
    android:id="@+id/button_yellow"
    android:layout_width="99dp"
    android:layout_height="30dp"
    app:layout_constraintTop_toBottomOf="@+id/button_green"
    app:layout_constraintBottom_toBottomOf="parent"
    android:text="   Yellow"
    android:background="#ffff00"
    android:layout_marginBottom="40dp"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="1dp"
    android:textOn="黄色"
    android:textOff=" " />

<Button
    android:layout_width="129dp"
    android:layout_height="48dp"
    android:text="Send"
    android:id="@+id/btn_send"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintTop_toTopOf="@+id/msg_input"
    app:layout_constraintLeft_toRightOf="@+id/msg_input"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    android:layout_marginTop="57dp" />

<EditText
    android:layout_width="275dp"
    android:layout_height="50dp"
    android:id="@+id/msg_input"
    tools:layout_constraintBottom_creator="1"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="1dp"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="460dp" />

 <ScrollView
    android:id="@+id/scrollView"
    android:layout_width="269dp"
    android:layout_height="0dp"
    android:layout_marginBottom="4dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="68dp"
    app:layout_constraintBottom_toTopOf="@+id/msg_input"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintTop_creator="1">


    <TextView
        android:id="@+id/textView"
        android:layout_width="394dp"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:scrollbars="vertical"
        android:textAppearance="?android:attr/textAppearanceLarge"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="49dp" />

 </ScrollView>


 </android.support.constraint.ConstraintLayout>

谢谢,非常感谢任何帮助!!

Logcat Error Message

1 个答案:

答案 0 :(得分:1)

首先尝试删除第56行的;。此外,看起来您没有初始化正确的对象。您将OnClickListener设置为“btn_send_msg”,但没有。

您需要提供一个按钮

    android:id="@+id/btn_send_msg"

然后在设置onClickListener之前将其初始化。 IE浏览器。取消注释:

    btn_send_msg = (Button) findViewById(R.id.btn_send);

然后把它放在:

    btn_send_msg.setOnClickListener(new View.OnClickListener() {...}

更新 - 将开关用作按钮

首先,初始化您的开关,即:

    my_switch = (Switch) findViewById(R.id.my_switch);

然后设置其OnClickListener

    my_switch.setOnClickListener(new View.OnClickListener(){...}

诀窍在于,您需要知道开关是否打开/关闭,以防需要以不同方式处理。因此,在OnClickListener方法中,执行以下操作:

boolean mSwitch = ((Switch) view).isChecked();

if(mSwitch){
//the switch is "on"
//do something
}
else {
//the switch is "off"
// do something else
}