以下是我的代码。请帮我在我的Android应用程序中添加进度条。我的应用程序是一种消息传递应用程序,我希望当我按下发送短信按钮时应显示循环进度条,进度条应该在收到短信时禁用。请查看我的代码并建议我该怎么做。请提及完整的细节,因为我是初学者,所以快捷方式的理解对我来说很难。三江源
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btnSendSMS;
EditText enteredNum;
IntentFilter intentFilter;
private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//---display the SMS received in the TextView---
TextView SMSes = (TextView) findViewById(R.id.textView1);
SMSes.setText(intent.getExtras().getString("sms"));
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//---intent to filter for SMS messages received---
intentFilter = new IntentFilter();
intentFilter.addAction("SMS_RECEIVED_ACTION");
btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
btnSendSMS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
enteredNum = (EditText) findViewById(R.id.enterDetail);
assert enteredNum != null;
sendSMS("51522", "HELP " + enteredNum.getText().toString().replaceAll(" ","").toUpperCase());
}
});
}
@Override
protected void onResume() {
//---register the receiver---
registerReceiver(intentReceiver, intentFilter);
super.onResume();
}
@Override
protected void onPause() {
//---unregister the receiver---
unregisterReceiver(intentReceiver);
super.onPause();
}
private void sendSMS(String phoneNumber, String message)
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
}
答案 0 :(得分:1)
您可以在activity_main.xml
中添加 ProgressBar 元素然后,在您的Activity中,您可以使用属性 setVisibility()来显示( View.VISIBLE )您的ProgressBar并隐藏( View.GONE < / em>)您的布局或内容。
祝你好运!